zoukankan      html  css  js  c++  java
  • Python时间,日期,时间戳之间转换

    1.将字符串的时间转换为时间戳
        方法:
            a = "2013-10-10 23:40:00"
            将其转换为时间数组
            import time
            timeArray = time.strptime(a, "%Y-%m-%d %H:%M:%S")
        转换为时间戳:
        timeStamp = int(time.mktime(timeArray))
        timeStamp == 1381419600
     
    2.字符串格式更改
        如a = "2013-10-10 23:40:00",想改为 a = "2013/10/10 23:40:00"
        方法:先转换为时间数组,然后转换为其他格式
        timeArray = time.strptime(a, "%Y-%m-%d %H:%M:%S")
        otherStyleTime = time.strftime("%Y/%m/%d %H:%M:%S", timeArray)
     
     
    3.时间戳转换为指定格式日期:
        方法一:
            利用localtime()转换为时间数组,然后格式化为需要的格式,如
            timeStamp = 1381419600
            timeArray = time.localtime(timeStamp)
            otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
            otherStyletime == "2013-10-10 23:40:00"
     
        方法二:
            import datetime
            timeStamp = 1381419600
            dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
            otherStyleTime = dateArray.strftime("%Y-%m-%d %H:%M:%S")
            otherStyletime == "2013-10-10 23:40:00"
     
    4.获取当前时间并转换为指定日期格式
        方法一:
            import time
            获得当前时间时间戳
            now = int(time.time())  ->这是时间戳
            转换为其他日期格式,如:"%Y-%m-%d %H:%M:%S"
            timeArray = time.localtime(timeStamp)
            otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
     
        方法二:
            import datetime
            获得当前时间
            now = datetime.datetime.now()  ->这是时间数组格式
            转换为指定的格式:
            otherStyleTime = now.strftime("%Y-%m-%d %H:%M:%S")
     
    5.获得三天前的时间
        方法:
            import time
            import datetime
            先获得时间数组格式的日期
            threeDayAgo = (datetime.datetime.now() - datetime.timedelta(days = 3))
            转换为时间戳:
                timeStamp = int(time.mktime(threeDayAgo.timetuple()))
            转换为其他字符串格式:
                otherStyleTime = threeDayAgo.strftime("%Y-%m-%d %H:%M:%S")
        注:timedelta()的参数有:days,hours,seconds,microseconds
     
    6.给定时间戳,计算该时间的几天前时间:
        timeStamp = 1381419600
        先转换为datetime
        import datetime
        import time
        dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
        threeDayAgo = dateArray - datetime.timedelta(days = 3)
        参考5,可以转换为其他的任意格式了
     

    Python 里的tm_isdst 

    DST 是daylight saving time, 意思是:夏令时

    在python的time, datetime模块下,按照struct_time格式输出时间,最后的一个tm_isdst的值就是告知是否为夏令时。

    tm_isdst = 1 的时候表示时间是夏令时,

        值为0的时候表示非夏令时

        值为-1的时候表示时间不确定是否是夏令时 

    获取本月的时间戳范围

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    # Author:Henry  17607168727@163.com
    import calendar
    import datetime
    import time
    
    
    def return_time_stamp():
        # 获取当天日期
        today = datetime.datetime.today()
        year = today.year
        month = today.month
        # 查询当月总天数
        month_end_days = calendar.monthrange(year,month)[1]
        # 构造本月第一天文本格式时间
        first_day_str = """%s-%s-%s 00:00:00""" % (year,month,1)
        # 构造本月最后一天文本格式时间
        end_day_str = """%s-%s-%s 00:00:00""" % (year,month,month_end_days)
        # 转成时间数组
        first_time_array = time.strptime(first_day_str, "%Y-%m-%d %H:%M:%S")
        end_time_array = time.strptime(end_day_str, "%Y-%m-%d %H:%M:%S")
        first_time_stamp = int(time.mktime(first_time_array))
        end_time_stamp = int(time.mktime(end_time_array))
        print(year,month,month_end_days,first_time_array,end_time_array,first_time_stamp,end_time_stamp)
        return [first_time_stamp,end_time_stamp]
    
    if __name__ == '__main__':
        
        return_time_stamp()
  • 相关阅读:
    iOS学习05C语言函数
    iOS学习04C语言数组
    iOS学习03C语言循环结构
    iOS学习02C语言分支结构
    iOS学习01C语言数据类型
    Objective-C学习——中文URL编码和解码
    Objective-c 字面量
    SDWebImage
    mac的svn之cornerstone简易教程
    javascript 和oc交互
  • 原文地址:https://www.cnblogs.com/jiangxiaobo/p/11703514.html
Copyright © 2011-2022 走看看