zoukankan      html  css  js  c++  java
  • time和datetime模块

    import time
    import datetime
    # #当前时间戳时
    # print(time.time())
    # #进程运行的实际时间
    # print(time.clock())
    # #睡眠时间
    # time.sleep(2)
    # #将一个时间戳转换成一个当前时区的时间元组(struct_time)
    print(time.localtime())
    # #>>time.struct_time(tm_year=2019, tm_mon=9, tm_mday=25, tm_hour=10, tm_min=24, tm_sec=22, tm_wday=2, tm_yday=268, tm_isdst=0)
    # #将一个时间戳转换成一个国际时区的时间元组(struct_time)
    print(time.gmtime())
    # #指定的格式化字符串输出
    print(time.strftime("%Y-%m-%d %H:%M:%S"))
    # #把当前时区的struct_time转换为指定格式化字符串输出
    # print(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()))

    #指定格式化字符串转化为时间元组(struct_time)
    print(time.strptime("2016-02-19 19:12:16","%Y-%m-%d %X"))
    # #将一个时间元组(struct_time)转换为时间戳
    # print(time.mktime(time.localtime()))

    #时间元组(time.struct_time)转化为固定格式的时间
    print(time.asctime(time.localtime(175254555.65)))
    #时间戳转化为固定格式的时间
    print(time.ctime(18884442.23))
    # ##date
    #新建一个date对象,日期为今天
    b=datetime.date.today()
    print(b.year)
    print(b.month)
    print(datetime.date.today())
    # #转化为日期形式
    # print(datetime.date(2017,8,15))
    # #格式化为需要的时间日期
    # print(datetime.date.today().strftime("%Y-%m-%d %H:%M:%S"))
    # #将日期形式转化为struct_time格式
    # print(datetime.date.today().timetuple())
    # #将替换返回来的值
    # print(datetime.date.today().replace(year=2136,month=5))
    # ##time
    # #将时间转换为字符串格式
    # print(datetime.time(11,12,5,12554).strftime("%Y-%m-%d %H:%M:%S"))
    # #1900-01-01 11:12:05
    # ##datetime
    # #获取当前日期和时间
    # print(datetime.datetime.now())
    # #获取今天日期和时间
    # print(datetime.datetime.today())
    # #把时间转化为指定字符串格式
    # print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
    # #str格式转datetime格式:
    # print(datetime.datetime.strptime("2018-09-25 11:17:51","%Y-%m-%d %H:%M:%S"))

    ##使用datetime.timedelta这个方法来前后移动时间,可用的参数有weeks,days,hours,minutes,seconds,microseconds等。
    # print(datetime.datetime.now() + datetime.timedelta(days=1))
    # 2018-06-19 16:49:48.574000
    # >>> print(datetime.datetime.now() + datetime.timedelta(hours=1))
    # 2018-06-18 17:49:57.122000
    # >>> print(datetime.datetime.now() + datetime.timedelta(minutes=-30))
    # 2018-06-18 16:20:08.619000
    ##获取两个时间的时间差(now是本地时间,可以认为是你电脑现在的时间;utcnow是世界时间<时区不同,所以这两个时间也是有所不同>)
    # print(datetime.datetime.now() - datetime.datetime.utcnow()).total_seconds()
    # 28800.0
  • 相关阅读:
    C# 生成随机索引列表
    QQ音乐MP3下载
    微信Dat文件解码
    C#工作常用关键字
    C#左移运算符
    C#中datatable操作
    html 显示 pdf
    framework7 下拉刷新、无限滚动
    framework7 总结之前遇到的问题和踩过的坑
    HTML 引用大全
  • 原文地址:https://www.cnblogs.com/liuwenwen/p/12911268.html
Copyright © 2011-2022 走看看