Conversión de cadena con varias fechas en un formato dd-mm-YYYY

I have a DB table with relation 1: N where N represents multiple dates for one event. The column with the dates is DateTime type, and I would like to keep the Time option for later use, but it won't be so bad if I have to change it to Date tipo.

The problem comes when I have to show those multiple dates in some GUI. I get the dates with the GROUP_CONCAT function which means that in JavaScript I operate with a string with comma-separated values representing the different dates, which by now is in the default SQL DateTime formato - YYYY-mm-dd hh:mm:ss.

Yo uso el split(',') function to get each date-time value and what I can do is to change the type of the SQL column to Date so when I split the string in JavaScript to end up with YYYY-mm-dd values. Which should be reversed to dd-mm-YYYY para GUI.

I'm not sure how to proceed here. I have in mind two main options:

First: Maybe there's a way to use dd-mm-YYYY format in SQL which will solve all the problems.

Second: some kind of (complex?!?) String manipulation in JavaScript to split the string of dates into an array with multiple elements and then try to format each element the way I need.

Honestly - I want to avoid the second option, but don't know if the first is possible, and maybe, there's another way that I haven't think of.

preguntado el 12 de junio de 12 a las 08:06

Just to make it clear for those that my try this with CodeIgniter and AR. To make this work you need to make second select just for this. Here is what I mean : $this->db->select("GROUP_CONCAT(DATE_FORMAT( mails_dates.dt_sending_date, '%d-%m-%Y')) AS sending_dates", FALSE ); -

2 Respuestas

Prueba esto ..

SELECT GROUP_CONCAT(DATE_FORMAT( date_time_column, '%d-%m-%Y' )) FROM test_table;

Respondido el 12 de junio de 12 a las 08:06

Looks like should do the trick but I get this error: Error Number: 1054, Unknown column '%d-%m-%Y' in 'field list'. Maybe it's in my syntax, I use CodeIgniter AR and the selections looks like this:GROUP_CONCAT(DATE_FORMAT( mails_dates.dt_sending_date, %d-%m-%Y )) AS sending_dates - Leron_says_get_back_Monica

you are using Mysql or something else? The syntax I have written is checked on mysql.. you can format it acc to you if you are using something else any api or tool. - manurajhada

phpmyadmin and CodeIgniter AR - Leron_says_get_back_Monica

No idea about CodeIgniter AR. You can run the query at phpmyadmin, if you are not getting desired result, please let me know. - manurajhada

yes I do, gonna accept your answer, I'm just waiting to see if anyone with CodeIgniter experience won't writer something, because obv. the problem is there. - Leron_says_get_back_Monica

First of all I advise to use the native Date/DateTime-format everywhere in your code and only use localized variations like dd-mm-yyyy only where you really want to display it. First reason: it is consistent in your code. Second reason: Sorting.


2012-03-01    >    01-03-2012
2012-05-12    >    12-05-2012
2012-01-03    >    03-01-2012 

Sorted by the native format you'll get...

1: 2012-01-03
2: 2012-03-01
3: 2012-05-12

Sorted by the output format dd-mm-yyyy it will look like this...

1: 01-03-2012
2: 03-01-2012
3: 12-05-2012

...and I doubt this is what most people want.

Adjusting the output via SQL

You can change the output in your SELECT-query , most RDBMS offer functions for this. For example, in MySQL it is DATE_FORMAT, que se ve así:


Adjusting the output via SQL

You can also change the output using php, which is explained aquí.

Adjusting the output via JavaScript

It isn't hard to do this in JavaScript, too: Aquí is a great SO post which explains it in detail.

contestado el 23 de mayo de 17 a las 12:05

Thanks gonna look though all your links, but I think for this case it wont be problem to use DATE_FORMAT, could you see my comment on the first answer and point where could the problem be in my SELECT statement for getting this error? - Leron_says_get_back_Monica

Escape the date format in DATE_FORMAT properly. Instead of DATE_FORMAT( mails_dates.dt_sending_date, %d-%m-%Y ) utilizado DATE_FORMAT( mails_dates.dt_sending_date, '%d-%m-%Y' )! - Bjoern

sadly, but that was my first thought too - don't work. Anyways, thanks a lot, there is a lot of useful info in your answer. Big thanks. - Leron_says_get_back_Monica

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.