• python:时间处理模块


    # coding=utf-8
    
    from datetime import date, datetime
    import time
    
    
    def date_test():
        print 'date.max', date.max
        print 'date.min', date.min
        print 'date.today', date.today()
        sdate = date.today()
        print sdate.year, sdate.month, sdate.day  # 获取日期的年月日
        print sdate.timetuple()  # 返回时间相关的数据结构
        print sdate.toordinal()  # 返回日期对应的Gregorian Calendar日期;
        print sdate.weekday()  # 实际星期几和真实星期差一天
        print sdate.isoweekday()  # 返回实际星期几
        print sdate.isocalendar()  # 返回一个日期元组
        print sdate.isoformat()  # 返回一个yyyy-mm-dd格式的日期
        sdate1 = sdate.replace(day=6)  # 生成指定时间的日期
        print sdate1
        diff = sdate1 - sdate
        print diff
        print sdate + diff
        print sdate1 > sdate
        
    def time_test():
        tm = time(23, 46, 10)
    #     print tm
    #     print time.max
    #     print time.min
        print tm.isoformat()
    
    def datetime_test():
        print 'datetime.max', datetime.max
        print 'datetime.min', datetime.min
        print 'datetime.resoluation', datetime.resolution  # 最小单位
        print 'datetime.today', datetime.today()
        print 'datetime.now', datetime.now()  # 可以指定时区
        print 'datetime.utcnow', datetime.utcnow()
        print 'datetime.fromtimestamp', datetime.fromtimestamp(time.time())  # 根据时间戳转化为时间
        
        sdatetime = datetime.today()  # 获取当天的时间日期
        print sdatetime.year  #
        print sdatetime.month  #
        print sdatetime.day  #
        print sdatetime.hour  #
        print sdatetime.minute  #
        print sdatetime.second  #
        print sdatetime.date()  # 日期
        print sdatetime.time()  # 时间
    
    def user_test():
        now = datetime.now()
        print now
        
        print now.strftime('%Y-%m-%d %H:%M:%S %f')  # 年,月,日,时,分,秒,毫秒
        print now.strftime('%Y-%m-%d %H:%M:%S %p')  # 年,月,日,时,分,秒,上午或者下午
        print now.strftime('%Y-%m-%d %H:%M')
        print now.strftime('%a')  # 星期几简写
        print now.strftime('%A')  # 星期几完整
        print now.strftime('%b')  # 几月简写
        print now.strftime('%B')  # 几月全写
        print now.strftime('%w')  # 这周的第几天
        print now.strftime('%j')  # 今年的第多少天
        print now.strftime('%U')  # 该周是今年的第几周
        print now.strftime('%X')  # 当前的时间
        print now.strftime('%x')  # 当前的日期
        print now.strftime('%c')  # 日期时间
        print now.ctime()
        
        print time.strftime('%Y-%m-%d %H:%M', time.localtime())  # 时间转化成字符串
        print type(time.strftime('%Y-%m-%d %H:%M', time.localtime()))
        print time.strptime("2015-02-18 12:10", '%Y-%m-%d %H:%M')
        print type(time.strptime("2015-02-18 12:10", '%Y-%m-%d %H:%M'))
        
    
    #python求两个时间的差
    def Caltime(date1, date2):
        date1 = datetime.strptime(date1, "%Y-%m-%d %H:%M")
        date2 = datetime.strptime(date2, "%Y-%m-%d %H:%M")
        print type(date2 - date1)
        diff = date2 - date1
        print diff.total_seconds()
        print diff.days
        print diff.seconds
        
        
        
    if __name__ == '__main__':
        Caltime("2012-08-10 06:15", "2012-08-10 06:26")
    else:
        pass
  • 相关阅读:
    关于伸缩盒子的使用问题
    html5前端框架
    ES6 promise对象
    Node和Electron开发入门(四):操作PC端文件系统
    兄弟组件的传值
    端口冲突解决办法
    查看mysql状态的常用命令
    使用mysqldump导入导出MySQL数据库
    Yii CModel中rules验证规则
    URL中#号的含义
  • 原文地址:https://www.cnblogs.com/dmir/p/5023362.html
Copyright © 2020-2023  润新知