MySQL日期处理

<PRE class=Java minmax_bound="true">1.  mysql> select now(),date(now()),sysdate();     
2.  +---------------------+-------------+---------------------+     
3.  | now()               | date(now()) | sysdate()           |     
4.  +---------------------+-------------+---------------------+     
5.  | 2008-12-02 10:11:36 | 2008-12-02  | 2008-12-02 10:11:36 |     
6.  +---------------------+-------------+---------------------+     
7.  1 row in set (0.00 sec)     
8.  mysql> select curdate(),curdate()+0,curtime(),curtime()+0;     
9.  +---------------------+-------------+---------------------+     
10. | curdate()  | curdate()+0 | curtime() | curtime()+0   |     
11. +---------------------+-------------+---------------------+     
12. | 2008-12-02 | 20081202    | 10:00:33  | 100033.000000 |     
13. +---------------------+-------------+---------------------+    
–返回日期当月最后一天  
浏览复制到剪贴板打印关于  
1.  mysql> select last_day('2008-12-02');     
2.  +------------------------+     
3.  | last_day('2008-12-02') |     
4.  +------------------------+     
5.  | 2008-12-31             |     
6.  +------------------------+     
7.  1 row in set (0.00 sec)    
–返回日期的星期几  
浏览复制到剪贴板打印关于  
1.  mysql> select dayname('2008-12-02'),dayofweek('2008-12-02');     
2.  +-----------------------+-------------------------+     
3.  | dayname('2008-12-02') | dayofweek('2008-12-02') |     
4.  +-----------------------+-------------------------+     
5.  | tuesday               |                       3 |     
6.  +-----------------------+-------------------------+     
7.  1 row in set (0.00 sec)    
 
–返回日期的年,月,日  
浏览复制到剪贴板打印关于  
1.  mysql> select month('2008-12-02'),year('2008-12-02'),day('2008-12-02');     
2.  +---------------------+--------------------+-------------------+     
3.  | month('2008-12-02') | year('2008-12-02') | day('2008-12-02') |     
4.  +---------------------+--------------------+-------------------+     
5.  |                  12 |               2008 |                 2 |     
6.  +---------------------+--------------------+-------------------+     
7.  1 row in set (0.00 sec)    
–返回日期的小时,分,秒  
浏览复制到剪贴板打印关于  
1.  mysql> select hour('10:05:03'),minute('10:05:03'),second('10:05:03');     
2.  +------------------+--------------------+--------------------+     
3.  | hour('10:05:03') | minute('10:05:03') | second('10:05:03') |     
4.  +------------------+--------------------+--------------------+     
5.  |               10 |                  5 |                  3 |     
6.  +------------------+--------------------+--------------------+     
7.  1 row in set (0.00 sec)    
1.subdate(d,t):起始时间加上一段时间(year,month,day…)  
浏览复制到剪贴板打印关于  
1.  mysql> select date_add('1998-01-02', interval 31 day),adddate('1998-01-02', 31);     
2.  +-----------------------------------------+---------------------------+     
3.  | date_add('1998-01-02', interval 31 day) | adddate('1998-01-02', 31) |     
4.  +-----------------------------------------+---------------------------+     
5.  | 1998-02-02                              | 1998-02-02                |     
6.  +-----------------------------------------+---------------------------+     
7.  1 row in set (0.00 sec)     
8.  mysql> select date_add('1998-01-02',interval 2 year);     
9.  +-----------------------------------------------------+     
10. | date_add('1998-01-02', interval 2 year)     
11. +-----------------------------------------------------+     
12. | 2000-01-02     
13. +-----------------------------------------------------+     
14. 1 row in set (0.00 sec)     
15. mysql> select date_add('1998-01-02', interval 2 hour);     
16. +-----------------------------------------------------+     
17. | date_add('1998-01-02', interval 2 hour)     
18. +-----------------------------------------------------+     
19. | 1998-01-02 02:00:00     
20. +-----------------------------------------------------+     
21. 1 row in set (0.00 sec)    
2.subdate(d,t):起始时间减去一段时间  
浏览复制到剪贴板打印关于  
1.  mysql> select subdate('1998-01-02', interval 31 day),subdate('1998-01-02', 31);     
2.  +----------------------------------------+---------------------------+     
3.  | subdate('1998-01-02', interval 31 day) | subdate('1998-01-02', 31) |     
4.  +----------------------------------------+---------------------------+     
5.  | 1997-12-02                             | 1997-12-02                |     
6.  +----------------------------------------+---------------------------+     
7.  1 row in set (0.00 sec)    
3.addtime(d,t):起始时间d加入时间t  
浏览复制到剪贴板打印关于  
1.  mysql> select addtime('1997-12-31 23:59:50','00:00:05'), addtime('23:59:50','00:00:05') ;     
2.  +-------------------------------------------+--------------------------------+     
3.  | addtime('1997-12-31 23:59:50','00:00:05') | addtime('23:59:50','00:00:05') |     
4.  +-------------------------------------------+--------------------------------+     
5.  | 1997-12-31 23:59:55                       | 23:59:55                       |     
6.  +-------------------------------------------+--------------------------------+     
7.  1 row in set (0.00 sec)    
4.subtime(d,t):起始时间d减去时间t  
浏览复制到剪贴板打印关于  
1.  mysql> select subtime('1997-12-31 23:59:50','00:00:05'), subtime('23:59:50','00:00:05');     
2.  +-------------------------------------------+--------------------------------+     
3.  | subtime('1997-12-31 23:59:50','00:00:05') | subtime('23:59:50','00:00:05') |     
4.  +-------------------------------------------+--------------------------------+     
5.  | 1997-12-31 23:59:45                       | 23:59:45                       |     
6.  +-------------------------------------------+--------------------------------+     
7.  1 row in set (0.00 sec)    
5.datediff(d1,d2):返回起始时间d1和结束时间d2之间的天数  
 
1.  mysql> select datediff('1997-12-31 23:59:59','1997-12-30');     
2.  +----------------------------------------------+     
3.  | datediff('1997-12-31 23:59:59','1997-12-30') |     
4.  +----------------------------------------------+     
5.  |                                            1 |     
6.  +----------------------------------------------+     
7.  1 row in set (0.00 sec)    
6.date_format(date,format):根据format字符串显示date值的格式  
 
1.  mysql> select date_format('2008-12-02 22:23:00', '%y %m %m %h:%i:%s');     
2.  +---------------------------------------------------------+     
3.  | date_format('2008-12-02 22:23:00', '%y %m %m %h:%i:%s') |     
4.  +---------------------------------------------------------+     
5.  | 2008 12 12 22:23:00                                     |     
6.  +---------------------------------------------------------+     
7.  1 row in set (0.00 sec)    
7.str_to_date(str,format) 字符串转化为时间  
mysql> select str_to_date(’04/31/2004′, ‘%m/%d/%y %h:%i:s’);  
+———————————————–+  
| str_to_date(’04/31/2004′, ‘%m/%d/%y %h:%i:s’) |  
+———————————————–+  
| 2004-04-31 00:00:00 |  
+———————————————–+  
1 row in set (0.00 sec)  
8.timestamp(expr) , timestamp(expr,expr2) :  
对于一个单参数,该函数将日期或日期时间表达式 expr 作为日期时间值返回.对于两个参数, 它将时间表达式 expr2添加到日期或日期时间表达式 expr 中,将theresult作为日期时间值返回  
 
1.  mysql> select timestamp('2003-12-31'), timestamp('2003-12-31 12:00:00','12:00:00');     
2.  +-------------------------+---------------------------------------------+     
3.  | timestamp('2003-12-31') | timestamp('2003-12-31 12:00:00','12:00:00') |     
4.  +-------------------------+---------------------------------------------+     
5.  | 2003-12-31 00:00:00     | 2004-01-01 00:00:00                         |     
6.  +-------------------------+---------------------------------------------+     
7.  1 row in set (0.00 sec)    
9.取当天0点0分,下一天0点0分  
1.  mysql> select timestamp(date(sysdate())),timestamp(adddate(date(sysdate()),1));     
2.  +----------------------------+---------------------------------------+     
3.  | timestamp(date(sysdate())) | timestamp(adddate(date(sysdate()),1)) |     
4.  +----------------------------+---------------------------------------+     
5.  | 2008-12-02 00:00:00        | 2008-12-03 00:00:00                   |     
6.  +----------------------------+---------------------------------------+     
7.  1 row in set (0.00 sec)  </PRE>  
<PRE class=Java minmax_bound="true"><A href="http://blog.csdn.net/superjava_2006/archive/2007/05/15/1609569.aspx" minmax_bound="true">http://blog.csdn.net/superjava_2006/archive/2007/05/15/1609569.aspx</A>  
 
</PRE> 

你可能感兴趣的:(mysql,.net,Blog)