python时间函数


 

1.时间的进制:年月日时分秒不说了

毫秒,符号ms(英语:millisecond ).
1毫秒等于一千分之一秒
微秒,符号μs(英语:microsecond ).
1微秒等于一百万分之一秒
1秒=1000毫秒(ms)
1毫秒=1000 微秒
2.取得时间
共有两个库:time和datetime,datetime是对象的封装。
time.clock()在win32下首次调用返回1微秒精度的时间,推荐使用。在Linux下返回CPU时间。
time.localtime()返回本地时间,但依赖于time.time()( 返回的是utc时间),在win32下的精度只有1/18.2秒,但在Linux下精度很高。
datetime.datetime.now() 返回当前时间。

下面是手册上说明:
time() 以浮点数返回纪元至今以来的秒数。
clock() 以浮点数返回CPU开始这个process的时间,(或者至上次调用这个函数的时间)
sleep() 延迟一段以浮点数表示的秒数。
gmtime() 把以秒表示的时间转换为通用协调时序列
localtime() 把秒时转换为本地时序列
asctime() 将时间序列转换成文本描述
ctime() 将秒时转换成文本描述
mktime() 将本地时序列转换成秒时
strftime() 以指定格式将序列时转为文本描述
strptime() 以指定格式从文本描述中解析出时间序列
tzset() 改变当地时区值
3.timedelta
datetime和 timedelta 可以做加减操作, timedelta 进行操作更直观,他们都有年月日时分秒的属性。
d1 = datetime.datetime.now()
d2 = d1 + datetime.timedelta(second=1) #增加1秒
print d2.second
4.strftime
格式化时间。这个函数在两个模块都存在。意义一致,就是调用方法不同。
datetime.datetime.strftime(nows,'%Y-%m-%d %H:%M:%S' )
time.strftime("%y%m%d%H%M%S",time.localtime())

DirectiveMeaningNotes
%aLocale’s abbreviated weekdayname. 
%ALocale’s full weekday name. 
%bLocale’s abbreviated monthname. 
%BLocale’s full month name. 
%cLocale’s appropriate date andtime representation. 
%dDay of the month as a decimalnumber [01,31]. 
%HHour (24-hour clock) as adecimal number [00,23]. 
%IHour (12-hour clock) as adecimal number [01,12]. 
%jDay of the year as a decimalnumber [001,366]. 
%mMonth as a decimal number[01,12]. 
%MMinute as a decimal number[00,59]. 
%pLocale’s equivalent of eitherAM or PM.(1)
%SSecond as a decimal number[00,61].(2)
%UWeek number of the year(Sunday as the first day ofthe week) as a decimal number[00,53]. All days in a newyear preceding the firstSunday are considered to be inweek 0.(3)
%wWeekday as a decimal number[0(Sunday),6]. 
%WWeek number of the year(Monday as the first day ofthe week) as a decimal number[00,53]. All days in a newyear preceding the firstMonday are considered to be inweek 0.(3)
%xLocale’s appropriate daterepresentation. 
%XLocale’s appropriate timerepresentation. 
%yYear without century as adecimal number [00,99]. 
%YYear with century as a decimalnumber. 
%ZTime zone name (no charactersif no time zone exists). 
%%A literal '%' character. 

                                                                                                                                     by:http://ideage.iteye.com/blog/645175

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值