mysql query - format date on output?

后端 未结 4 1614
情书的邮戳
情书的邮戳 2021-01-16 06:57

In my table, dates are stored like this: 2011-03-03T13:30:00

I\'m trying to output dates like this: March 3, 2011 1:30 PM

I\'d much rather work it into the q

4条回答
  •  臣服心动
    2021-01-16 07:35

    You basically have two different operations you may need to perform when handling dates: date to string and vice versa. The functions you can use are DATE_FORMAT() and STR_TO_DATE(). Full reference can be found in the manual.

    Usage example:

    SELECT
        DATE_FORMAT(CURRENT_TIMESTAMP, '%d/%m/%Y %H:%i:%s'),
        STR_TO_DATE('31/12/2001 23:55:00', '%d/%m/%Y %H:%i:%s')
    

    If your dates are not real dates but strings, you'll need to convert twice: from string to date and again from date to string:

    SELECT
        STR_TO_DATE('2011-03-03T13:30:00', '%Y-%m-%dT%H:%i:%s'),
        DATE_FORMAT(STR_TO_DATE('2011-03-03T13:30:00', '%Y-%m-%dT%H:%i:%s'), '%M %e, %Y %l:%i %p')
    

提交回复
热议问题