python datetime模块相关 mysql 时间戳等
2017-08-29 10:54
288 查看
(转载)http://blog.csdn.net/power0405hf/article/details/48574045
原文在此
所有日期,时间的API都在datetime模块内。
2
3
4
1
2
3
4
strptime
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
1
2
3
4
5
6
select unix_timestamp(‘2009-10-26 10-06-07’)
如果参数为空,则处理为当前时间
select from_unixtime(1256540102)
//------------------------------------------------------------------------------------------------
now = datetime.datetime.now()
nowStr = now.strftime('%Y-%m-%d %H:%M:%S')相当于mysql中的now()函数
原文在此
所有日期,时间的API都在datetime模块内。
1.日期输出格式化
strftime()函数将datetime结构格式化为一个字符串 datetime =>string now = datetime.datetime.now() now.strftime('%Y-%m-%d %H:%M:%S')1
2
3
4
1
2
3
4
转换控制符 | 说明 |
---|---|
%a | 星期几的简写形式 |
%A | 星期几的全称 |
%b | 月份的简写形式 |
%B | 月份的全称 |
%c | 日期和时间 |
%d | 月份中的日期,0-31 |
%H | 小时,00-23 |
%I | 12进制小时钟点,01-12 |
%j | 年份中的日期,001-366 |
%m | 年份中的月份,01-12 |
%M | 分,00-59 |
%p | 上午或下午 |
%S | 秒,00-60 |
%u | 星期几,1-7 |
%w | 星期几,0-6 |
%x | 当地格式的日期 |
%X | 当地格式的时间 |
%y | 年份中的最后两位数,00-99 |
%Y | 年 |
%Z | 地理时区名称 |
import time t = time.time() print t 1202872416.4920001 print type(t) <type 'float'> t = time.localtime() print t (2008, 2, 13, 10, 56, 44, 2, 44, 0) print type(t) <type 'time.struct_time'> print time.strftime('%Y-%m-%d', t) '2008-02-13' print time.strptime('2008-02-14', '%Y-%m-%d') (2008, 2, 14, 0, 0, 0, 3, 45, -1)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2.日期转为时间戳
#设a为字符串 import time a = "2011-09-28 10:00:00" #将"2011-09-28 10:00:00"转化为时间戳 time.mktime(time.strptime(a,'%Y-%m-%d %H:%M:%S')) 1317091800.01
2
3
4
5
6
1
2
3
4
5
6
3.mysql 时间
1、将时间转换为时间戳
select unix_timestamp(‘2009-10-26 10-06-07’)如果参数为空,则处理为当前时间
2、将时间戳转换为时间
select from_unixtime(1256540102)//------------------------------------------------------------------------------------------------
now = datetime.datetime.now()
nowStr = now.strftime('%Y-%m-%d %H:%M:%S')相当于mysql中的now()函数
相关文章推荐
- python datetime模块相关 mysql 时间戳等
- Python中time和datetime模块对时间的操作
- Python日期时间模块datetime详解与Python 日期时间的比较,计算实例代码
- python 时间模块: datetime
- python datetime模块(1) 时间格式化
- python datetime模块用strftime 格式化时间
- Python时间模块datetime、time、calendar的使用方法
- python datetime模块用strftime 格式化时间
- python 学习笔记 14 -- 常用的时间模块之datetime
- Python模块整理(二):时间相关模块time
- Python的时间模块datetime详解
- python datetime模块用strftime 格式化时间
- python datetime模块用strftime 格式化时间
- Python模块知识:时间日期日历模块Time、Datetime、Calendar
- Python3 日期时间 相关模块(time(时间) / datatime(日期时间) / calendar(日历))
- python datetime模块用strftime 格式化时间
- Python3之时间模块time & datetime & calendar
- python 时间模块 time 与 datetime 小结
- Python之日期与时间处理模块(date和datetime)
- python的time和datetime模块 之 时间形式互转