time——python

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_30648823/article/details/79916734

Python中,通常有这几种方式来表示时间:

  • 时间戳 
  • 格式化的时间字符串 
  • 元组(struct_time)(共九个元素)

UTC格林威治天文时间,世界标准时间。

UTC+8 中国

DST 夏令时


时间戳:

        时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。

        返回时间戳方式的函数主要有time(),clock()


元组:struct_time元组共有9个元素

扫描二维码关注公众号,回复: 5097589 查看本文章

        返回struct_time的函数主要有gmtime(),localtime(),strptime()

  • time
import time

#打印时间搓
print(time.time())
#标准时间(utc)(可以传入时间搓)得到元祖
print(time.gmtime())
#加入时区之后的时间(utc+8)(可以传入时间搓)得到元祖
print(time.localtime())

#将元祖转换为时间搓
t = time.gmtime()
print(time.mktime(t))

#将时间搓转换为str
print(time.strftime("%y_%m_%d   %H:%M:%S",time.localtime()))
#将str转换为时间搓
print(time.strptime("18_04_12   16:44:31","%y_%m_%d   %H:%M:%S"))

#直接输出时间
print(time.ctime())
print(time.asctime())

  • datetime
    import datetime
    #打印完整格式的时间 2018-04-12 17:16:55.646589
    print(datetime.datetime.now())
    #打印日期的形式 2018-04-12
    print(datetime.date.fromtimestamp(time.time()))
    #加减时间2018-04-17 17:20:04.679652
    print(datetime.datetime.now()+datetime.timedelta(5))
    print(datetime.datetime.now()+datetime.timedelta(-5))
    print(datetime.datetime.now()+datetime.timedelta(minutes=20))
    print(datetime.datetime.now()+datetime.timedelta(hours=2))

猜你喜欢

转载自blog.csdn.net/qq_30648823/article/details/79916734
今日推荐