zoukankan      html  css  js  c++  java
  • python时间处理 time,datetime,arrow

    内置time

    import time
    
    # 时间戳
    # print(time.time()) 
    # 时间戳转换系统时间 2018-04-04 11:00:55 # ctime = time.localtime(time.time()) # print(time.strftime('%Y-%m-%d %H:%M:%S',ctime)) # 睡1秒 (秒单位) # print(time.sleep(1)) # 输出当前系统时间 # x = time.localtime() # t = time.strftime('%Y-%m-%d %H:%M:%S', x) # 按格式输入当前时间 , x = 元组 # print(t) # 2018-02-06 16:37:30
    # 转换时间数组
    # dt = "2016-05-05 20:28:54"
    # dt2 = time.strptime(dt,"%Y-%m-%d %H:%M:%S")
    # time.struct_time(tm_year=2016, tm_mon=5, tm_mday=5, tm_hour=20, tm_min=28, tm_sec=54, tm_wday=3, tm_yday=126, tm_isdst=-1)
    # 在转换日期时间
    # dt3 = time.strftime("%Y-%m-%d %H:%M:%S",dt2)
    # print(dt3)
    

      

    内置datetime 可以做日期加减

    # 对time模块的封装
    import datetime
    
    # %Y-%m-%d %H:%M:%S
    # 年 月  日 时 分 秒
    # 当前日期
    # print('当前日期:', datetime.datetime.now())  # 当前日期: 2018-04-04 11:04:46.090568
    # 日期相加 (3天后的日期)
    # print(datetime.datetime.now() + datetime.timedelta(3))  # 2018-04-07 11:04:46.090568
    # 日期相减 3天前
    # print(datetime.datetime.now() + datetime.timedelta(-3))
    # 小时相加 , -3减
    # print(datetime.datetime.now() + datetime.timedelta(hours=3))
    
    # 时间替换
    # c_time = datetime.datetime.now()
    # print(c_time.replace(year=2019))  # 2019-04-04 11:29:47.653526
    
    # 数据库查询返回的时间类型,转换位 时分
    # t = datetime.datetime(2018, 4, 4, 10, 48, 59, 793000).strftime('%H:%M')  # 10:48
    # print(t)
    
    
    # t_str = '2012-03-05 16:26:23'
    # 将字符串转换为datetime 日期 string => datetime
    # d = datetime.datetime.strptime(t_str, '%Y-%m-%d %H:%M:%S')
    # 这样就可以进行加减了
    # print(d + datetime.timedelta(-3))
    
    # 2个日期比较
    # d1 = datetime.datetime.strptime('2012-03-05 17:41:20', '%Y-%m-%d %H:%M:%S')
    # d2 = datetime.datetime.strptime('2012-03-02 17:41:20', '%Y-%m-%d %H:%M:%S')
    # delta = d1 - d2
    # print(delta.days)
    

    Arrow 库

    安装   pip install arrow

    import arrow
    
    # 获取当前时间
    now2 = arrow.utcnow()  # 时间戳 timestamp
    # print(now2.timestamp)
    #
    now = arrow.now()
    print(now.timestamp)  # 时间戳
    # t = now.format('YYYY-MM-DD HH:mm')  # 2018-04-04 09:39
    # print('当前时间',t)
    # mm = start.format('HH:mm')  # 09:48
    # 当前时间加1分钟
    # n = now.shift(minutes=1)
    # print('当前时间加1分钟',n.format('YYYY-MM-DD HH:mm'))
    # 当前时间加1小时
    # n = now.shift(hours=1)
    # print('当前时间加1小时',n.format('YYYY-MM-DD HH:mm'))
    # n = now.shift(months=1)
    # print('当前时间加1月',n.format('YYYY-MM-DD HH:mm'))
    # n = now.shift(years=1)
    # print('当前时间加1年',n.format('YYYY-MM-DD HH:mm'))
    # n = now.shift(days=1)
    # print('当前时间加1天',n.format('YYYY-MM-DD HH:mm'))
    # n = now.shift(days=-1)
    # print('当前时间加-1天',n.format('YYYY-MM-DD HH:mm'))
    # n = now.shift(weeks=1)
    # print('当前时间加1周',n.format('YYYY-MM-DD HH:mm'))
    
    # ---------------- 转换arrow对象
    # 字符串转换arrow对象
    # strarr = arrow.get("2017-01-20 11:30", "YYYY-MM-DD HH:mm")
    # year = strarr.format('YYYY')
    # print(year)
    # 时间戳转换arrow对象
    # sjc = arrow.get("1485937858.659424").format("YYYY-MM-DD")
    # print(sjc)
    # 直接生产一个Arrow对象
    # aw = arrow.Arrow(2017, 2, 1)
    # print(aw.format('YYYY:MM-DD HH:mm'))
    
  • 相关阅读:
    14 微服务电商【黑马乐优商城】:day02-springcloud(理论篇一:HttpClient的简单使用)
    14 微服务电商【黑马乐优商城】:day01-springboot(Thymeleaf快速入门)
    14 微服务电商【黑马乐优商城】:day01-springboot(理论篇)
    1.1 行列式:二阶三阶n阶行列式
    ubuntu:make工程管理器大致了解
    ubuntu:VIM使用
    机器学习:朴素贝叶斯邮件分类(python实现)
    ubuntu 添加快速启动
    ubuntu:软件包
    Ubuntu:远程管理ubuntu文件系统
  • 原文地址:https://www.cnblogs.com/412013cl/p/8716550.html
Copyright © 2011-2022 走看看