Mysql 日期时间 DATE_FORMAT(date,format)

本文转自:http://dev.mysql.com/doc/refman/4.1/en/date-and-time-functions.html#function_date-format

  • DATE_FORMAT(date,format)

    Formats the date value according to the format string.

    The following specifiers may be used in the format string. As of MySQL 3.23, the “%” character is required before format specifier characters. In earlier versions of MySQL, “%” was optional.








































































































    Specifier Description
    %a Abbreviated weekday name (Sun..Sat)
    %b Abbreviated month name (Jan..Dec)
    %c Month, numeric (0..12)
    %D Day of the month with English suffix (0th, 1st, 2nd, 3rd, …)
    %d Day of the month, numeric (00..31)
    %e Day of the month, numeric (0..31)
    %f Microseconds (000000..999999)
    %H Hour (00..23)
    %h Hour (01..12)
    %I Hour (01..12)
    %i Minutes, numeric (00..59)
    %j Day of year (001..366)
    %k Hour (0..23)
    %l Hour (1..12)
    %M Month name (January..December)
    %m Month, numeric (00..12)
    %p AM or PM
    %r Time, 12-hour (hh:mm:ss followed by AM or PM)
    %S Seconds (00..59)
    %s Seconds (00..59)
    %T Time, 24-hour (hh:mm:ss)
    %U Week (00..53), where Sunday is the first day of the week
    %u Week (00..53), where Monday is the first day of the week
    %V Week (01..53), where Sunday is the first day of the week; used with %X
    %v Week (01..53), where Monday is the first day of the week; used with %x
    %W Weekday name (Sunday..Saturday)
    %w Day of the week (0=Sunday..6=Saturday)
    %X Year for the week where Sunday is the first day of the week, numeric, four digits; used with %V
    %x Year for the week, where Monday is the first day of the week, numeric, four digits; used with %v
    %Y Year, numeric, four digits
    %y Year, numeric (two digits)
    %% A literal “%” character
    %x x, for any “x” not listed above

    The %v, %V, %x, and %X format specifiers are available as of MySQL 3.23.8. %f is available as of MySQL 4.1.1.

    Ranges for the month and day specifiers begin with zero due to the fact that MySQL permits the storing of incomplete dates such as '2014-00-00' (as of MySQL 3.23).

    As of MySQL 4.1.21, the language used for day and month names and abbreviations is controlled by the value of the lc_time_names system variable (Section 9.8, “MySQL Server Locale Support”).

    As of MySQL 4.1.23, DATE_FORMAT() returns a string with a character set and collation given by character_set_connection and collation_connection so that it can return month and weekday names containing non-ASCII characters. Before 4.1.23, the return value is a binary string.

    mysql> SELECT DATE_FORMAT('2009-10-04 22:23:00', '%W %M %Y');
    -> 'Sunday October 2009'
    mysql> SELECT DATE_FORMAT('2007-10-04 22:23:00', '%H:%i:%s');
    -> '22:23:00'
    mysql> SELECT DATE_FORMAT('1900-10-04 22:23:00',
    -> '%D %y %a %d %m %b %j');
    -> '4th 00 Thu 04 10 Oct 277'
    mysql> SELECT DATE_FORMAT('1997-10-04 22:23:00',
    -> '%H %k %I %r %T %S %w');
    -> '22 22 10 10:23:00 PM 22:23:00 00 6'
    mysql> SELECT DATE_FORMAT('1999-01-01', '%X %V');
    -> '1998 52'
    mysql> SELECT DATE_FORMAT('2006-06-00', '%d');
    -> '00'
  • (0)

    相关推荐

    • mysql 日期和时间格式转换实现语句

      这里是一个使用日期函数的例子.下面的查询选择了所有记录,其date_col的值是在最后30天以内: mysql> SELECT something FROM table WHERE TO_DAYS(NOW()) - TO_DAYS(date_col) <= 30; DAYOFWEEK(date) 返回日期date的星期索引(1=星期天,2=星期一, --7=星期六).这些索引值对应于ODBC标准. mysql> select DAYOFWEEK('1998-02-03'); ->

    • MySql用DATE_FORMAT截取DateTime字段的日期值

      用 DATE_FORMAT 来格式化日期字段 SELECT DATE_FORMAT(crt_time,'%Y-%m-%d') FROM ad_n_advertise_t

    • MySQL中日期和时间戳互相转换的函数和方法

      ① 时间戳转换成日期 复制代码 代码如下: FROM_UNIXTIME 例如: 数据表中 invest_time 存储的是时间戳,如 1429063399 使用 FROM_UNIXTIME 可以把时间戳转换为日期: 复制代码 代码如下: select FROM_UNIXTIME(invest_time,'%Y年%m月%d') from crm_invest_apply 执行结果: ② 把日期转换为时间戳,和 FROM_UNIXTIME 正好相反 复制代码 代码如下: UNIX_TIMESTAMP

    • PHP+Mysql日期时间如何转换(UNIX时间戳和格式化日期)

      写过PHP+MySQL的程序员都知道有时间差,UNIX时间戳和格式化日期是我们常打交道的两个时间表示形式,Unix时间戳存储.处理方便,但是不直观,格式化日期直观,但是处理起来不如Unix时间戳那么自如,所以有的时候需要互相转换,下面给出互相转换的几种转换方式. 一.在MySQL中完成 这种方式在MySQL查询语句中转换,优点是不占用PHP解析器的解析时间,速度快,缺点是只能用在数据库查询中,有局限性. 1. UNIX时间戳转换为日期用函数: FROM_UNIXTIME() 一般形式:selec

    • MySQL日期数据类型、时间类型使用总结

      MySQL 日期类型:日期格式.所占存储空间.日期范围 比较. 日期类型        存储空间       日期格式                 日期范围 ------------ ---------   --------------------- ----------------------------------------- datetime       8 bytes   YYYY-MM-DD HH:MM:SS   1000-01-01 00:00:00 ~ 9999-12-31

    • mysql 获取当前日期函数及时间格式化参数详解

      MYSQL 获取当前日期及日期格式 获取系统日期: NOW() 格式化日期: DATE_FORMAT(date, format) 注: date:时间字段 format:日期格式 返回系统日期,输出 2009-12-25 14:38:59 select now(); 输出 09-12-25 select date_format(now(),'%y-%m-%d'); 根据format字符串格式化date值: %S, %s 两位数字形式的秒( 00,01, ..., 59) %I, %i 两位数字形

    • MySql日期查询语句详解

      使用DATE_FORMAT方法SELECT * FROM `ler_items` WHERE DATE_FORMAT(postTime,'%Y-%m')='2013-03'注意:日期一定要用'',否则没有效果其它的一些关于mysql日期查找语句mysql> select date_format(DATE_SUB(CURDATE(), INTERVAL 7 DAY),'%y%m%d');+-------------------–+| date_format(DATE_SUB(CURDATE(),

    • mysql中格式化日期详解

      1. DATE_FORMAT() 函数用于以不同的格式显示日期/时间数据. DATE_FORMAT(date,format) format参数的格式有 %a 缩写星期名 %b 缩写月名 %c 月,数值 %D 带有英文前缀的月中的天 %d 月的天,数值(00-31) %e 月的天,数值(0-31) %f 微秒 %H 小时 (00-23) %h 小时 (01-12) %I 小时 (01-12) %i 分钟,数值(00-59) %j 年的天 (001-366) %k 小时 (0-23) %l 小时 (

    • mysql中取系统当前时间,当前日期方便查询判定的代码

      php中常通过下面的代码,得到判定日期的sql查询语句 复制代码 代码如下: $now = time(); //获取当期的日期 $sql="select * from `team` where end_time>$now ORDER BY sort_order limit 0,4"; 获取当前时间的MySql时间函数 处理MySql时间日期的函数有很多,下面为您介绍的就是用于获取当前时间的MySql时间函数,如果您对此感兴趣的话,不妨一看 下面为您介绍的MySql时间函数用于获取

    • MySQL的Data_ADD函数与日期格式化函数说明

      DATE_ADD(date,INTERVAL expr type) DATE_SUB(date,INTERVAL expr type) 这些函数执行日期运算. date 是一个 DATETIME 或DATE值,用来指定起始时间. expr 是一个表达式,用来指定从起始日期添加或减去的时间间隔值.  Expr是一个字符串;对于负值的时间间隔,它可以以一个 '-'开头. type 为关键词,它指示了表达式被解释的方式. 关键词INTERVA及 type 分类符均不区分大小写. 以下表显示了type

    随机推荐