zoukankan      html  css  js  c++  java
  • Python获取前几天的日期转成固定的格式,Python时间、日期、时间戳的转换

    1、str与datetime.datetime类型之间的切换,如下所示:

    >>> stime = "2020-05-06"
    >>> type(stime)
    <class 'str'>
    >>> dt = datetime.strptime(stime,"%Y-%m-%d")
    >>> type(dt)
    <class 'datetime.datetime'>
    >>> strtime = dt.strftime("%Y-%m-%d")
    >>> type(strtime)
    <class 'str'>

    str转换成datetime.datetime类型:datetime.strptime()函数。将str类型stime转换成datetime.datetime类型。

    datetime.datetime类型转化成str:dt.strftime().将原本是datetime.datetime类型的数据转换成str类型。

    相互为datetime.datetime类型的数据才能进行加减等操作,

    应用:

    2,获取8天之前的数据

    代码如下:

    import time
    import datetime
     
    today = time.strftime("%Y-%m-%d", time.localtime())  /获取当前时间并转换成固定格式
    week_ago_time = (datetime.datetime.now() - datetime.timedelta(days=8)).strftime("%Y-%m-%d")  /获取8天前的日期

    3,获取固定日期的前后n天的数据

    先将固定日期转化成'datetime.datetime'格式的,因为'datetime.datetime.now()'就是这种格式的;然后想上面一样做减法就可以了、

    这里用到的函数是datetime.datetime库的strptime(),代码如下所示:

    import time
    import datetime 
    from datetime import datetime as dtime
    dt = dtime.strptime("2019-07-07", "%Y-%m-%d")
    week_ago_time = ( dt - datetime.timedelta(days=8)).strftime("%Y-%m-%d")

    注意:from datetime import datetime as dtime 将库的名字做个替换不然会跟其父库datetime冲突

    输出为:

    >>> print week_ago_time
    2019-06-29

    3,计算两个日期的相差的天数

    >>> day1 = "2020-05-06"
    >>> day2 = "2020-05-01"
    >>> day1_dt = datetime.strptime(day1,"%Y-%m-%d")
    >>> day2_dt = datetime.strptime(day2,"%Y-%m-%d")
    >>> (day1_dt-day2_dt).days
    5

    先将两个str类型的日期转换成datetime类型;然后作减法,其days即相差天数。

    参考:https://blog.csdn.net/w417950004/article/details/92848984

    python中时间、日期、时间戳的转换

    1.简介

    在编写代码时,往往涉及时间、日期、时间戳的相互转换。

    2.示例

    # 引入模块
    import time, datetime

    2.1 str类型的日期转换为时间戳

    # 字符类型的时间
    tss1 = '2013-10-10 23:40:00'
    # 转为时间数组
    timeArray = time.strptime(tss1, "%Y-%m-%d %H:%M:%S")
    print timeArray
    # timeArray可以调用tm_year等
    print timeArray.tm_year   # 2013
    # 转为时间戳
    timeStamp = int(time.mktime(timeArray))
    print timeStamp  # 1381419600
    
    
    # 结果如下
    time.struct_time(tm_year=2013, tm_mon=10, tm_mday=10, tm_hour=23, tm_min=40, tm_sec=0, tm_wday=3, tm_yday=283, tm_isdst=-1)
    2013
    1381419600

    2.2 更改str类型日期的显示格式

    tss2 = "2013-10-10 23:40:00"
    # 转为数组
    timeArray = time.strptime(tss2, "%Y-%m-%d %H:%M:%S")
    # 转为其它显示格式
    otherStyleTime = time.strftime("%Y/%m/%d %H:%M:%S", timeArray)
    print otherStyleTime  # 2013/10/10 23:40:00
    
    tss3 = "2013/10/10 23:40:00"
    timeArray = time.strptime(tss3, "%Y/%m/%d %H:%M:%S")
    otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
    print otherStyleTime  # 2013-10-10 23:40:00

    2.3 时间戳转换为指定格式的日期

    # 使用time
    timeStamp = 1381419600
    timeArray = time.localtime(timeStamp)
    otherStyleTime = time.strftime("%Y--%m--%d %H:%M:%S", timeArray)
    print(otherStyleTime)   # 2013--10--10 23:40:00
    # 使用datetime
    timeStamp = 1381419600
    dateArray = datetime.datetime.fromtimestamp(timeStamp)
    otherStyleTime = dateArray.strftime("%Y--%m--%d %H:%M:%S")
    print(otherStyleTime)   # 2013--10--10 23:40:00
    # 使用datetime,指定utc时间,相差8小时
    timeStamp = 1381419600
    dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
    otherStyleTime = dateArray.strftime("%Y--%m--%d %H:%M:%S")
    print(otherStyleTime)   # 2013--10--10 15:40:00

    2.4 获取当前时间并且用指定格式显示

    # time获取当前时间戳
    now = int(time.time())     # 1533952277
    timeArray = time.localtime(now)
    print timeArray
    otherStyleTime = time.strftime("%Y--%m--%d %H:%M:%S", timeArray)
    print otherStyleTime
    
    # 结果如下
    time.struct_time(tm_year=2018, tm_mon=8, tm_mday=11, tm_hour=9, tm_min=51, tm_sec=17, tm_wday=5, tm_yday=223, tm_isdst=0)
    2018--08--11 09:51:17
    
    
    # datetime获取当前时间,数组格式
    now = datetime.datetime.now()
    print now
    otherStyleTime = now.strftime("%Y--%m--%d %H:%M:%S")
    print otherStyleTime
    
    # 结果如下:
    2018-08-11 09:51:17.362986
    2018--08--11 09:51:17

    参考:https://www.cnblogs.com/jfl-xx/p/8024596.html

  • 相关阅读:
    HTML5-Canvas 初认识
    JProfiler入门笔记
    Java 类加载与初始化
    JAVA责任链设计模式
    JAVA观察者模式
    JAVA模板方法模式
    JAVA策略模式
    JAVA装饰器模式
    Java设计模式--------建造者模式(Builder模式)
    供应链电子商务
  • 原文地址:https://www.cnblogs.com/T8888/p/13927094.html
Copyright © 2011-2022 走看看