python 3 time/datetime模块

一. time 模块

1. 这个模块提供多种函数来操作时间

2. 时间三种表示方式:

  1. 格式化的字符串

  2. 时间戳

  3. 时间元组

3. 三种格式之间转换关系:

 

4. 时间元组解释

The other representation is a tuple of 9 integers giving local time.The tuple items are:

  year (including century, e.g. 1998)
  month (1-12)
  day (1-31)
  hours (0-23)
  minutes (0-59)
  seconds (0-59)
  weekday (0-6, Monday is 0)
  Julian day (day in the year, 1-366)
  DST (Daylight Savings Time) flag (-1, 0 or 1)

5. time内部变量

timezone -- difference in seconds between UTC and local standard time
altzone -- difference in seconds between UTC and local DST time
daylight -- whether local time should reflect DST
tzname -- tuple of (standard time zone name, DST time zone name)

print(time.daylight)    #utc时间与本地时间的秒数
print(time.altzone)     #utf时间与夏日节约时间相差的秒数
print(time.timezone)  #是否使用夏季节约时间

 

6.内部函数

time() -- return current time in seconds since the Epoch as a float
clock() -- return CPU time since process start as a float
sleep() -- delay for a number of seconds given as a float
gmtime() -- convert seconds since Epoch to UTC tuple
localtime() -- convert seconds since Epoch to local time tuple
asctime() -- convert time tuple to string
ctime() -- convert time in seconds to string
mktime() -- convert local time tuple to seconds since Epoch
strftime() -- convert time tuple to string according to format specification
strptime() -- parse string to time tuple according to format specification
tzset() -- change the local timezone

print(time.time())#返回当前时间戳,UTC时区
time.sleep(1)
print('gmtime', time.gmtime()) #将时间戳转换成UTC时间元组
print(time.localtime()) #将时间戳转换成本地时间元组
print(time.asctime(time.localtime()))#将元组转换成字符串
print(time.ctime())#将时间串转换成字符串
print(time.mktime(time.gmtime())) #将本地时间转换成时间戳
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))#将时间元组转换成格式化字符串
print(time.strptime( '2017-09-08 23:11:11', '%Y-%m-%d %H:%M:%S'))#将字符串时间格式转为时间元组

 

二.  datetime模块

datetime是Python处理日期和时间的标准库

#获取当前时间
print(datetime.datetime.now())

#获取当前日期
print(datetime.date.today())

#2016-01-16 将时间戳转成日期格式
print(datetime.date.fromtimestamp(time.time()-864400))

t = datetime.datetime.now()
#输出2012-2-12 15:47:56.183790,返回当前时间,但指定的值将被替换
print(t.replace(2013,2,12))

new_date = datetime.datetime.now() + datetime.timedelta(days=10) #比现在加10天
new_date = datetime.datetime.now() + datetime.timedelta(days=-10) #比现在减10天
new_date = datetime.datetime.now() + datetime.timedelta(hours=-10) #比现在减10小时
new_date = datetime.datetime.now() + datetime.timedelta(seconds=120) #比现在+120s

datetime转换为timestamp

td = datetime.datetime(2017, 10, 27, 15, 52)
print(td.timestamp())

在Python中的时间戳是一个浮点小数,如果有小数位,小数位表示毫秒数。

timestamp 转换为 datetime

t = 1490761200.0
print(datetime.datetime.fromtimestamp(t))  # 本地时间转换
print(datetime.datetime.utcfromtimestamp(t))  # UTC时间转换

 

转载于:https://www.cnblogs.com/tonysmith/p/7743796.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值