zoukankan      html  css  js  c++  java
  • python时间戳,获取当前时间,时间格式转换,求出前几天或后几天的时间

    import time
    import datetime
    import locale
    import  random
    
    class TimeUtil:
    
        def __init__(self, curtime=None):
            self.curtime = curtime
    
        def get_timestemp(self):
            return time.time()
    
        def get_date(self):
            return time.strftime("%Y-%m-%d")
    
        def get_time(self):
            return time.strftime("%H:%M:%S")
    
        def get_datetime(self):
            return time.strftime("%Y-%m-%d %H:%M:%S")
    
        def get_chinesedate(self):
            locale.setlocale(locale.LC_ALL, 'en')
            locale.setlocale(locale.LC_CTYPE, 'chinese')
            strTime = time.strftime("%Y年%m月%d日", time.localtime())
            return strTime
    
        def get_chinesetime(self):
            locale.setlocale(locale.LC_ALL, 'en')
            locale.setlocale(locale.LC_CTYPE, 'chinese')
            strTime = time.strftime("%H时%M分%S秒", time.localtime())
            return strTime
    
        def get_chinesedatetime(self):
            locale.setlocale(locale.LC_ALL, 'en')
            locale.setlocale(locale.LC_CTYPE, 'chinese')
    
            strTime = time.strftime("%Y年%m月%d日%H时%M分%S秒", time.localtime())
            return strTime
    
        def compute_date(self, day_interval):
            # 获取今天的日期
            today = datetime.date.today()
            # 在今天的日期上再减10天
            if isinstance(day_interval, int) and day_interval >= 0:
                return today + datetime.timedelta(days=day_interval)
            elif isinstance(day_interval, int) and day_interval < 0:
                return today - datetime.timedelta(days=abs(day_interval))
    
        def timestamp_to_date(self, timestamp):
            if not isinstance(timestamp, (int, float)):
                return None
            locale.setlocale(locale.LC_CTYPE, 'chinese')
            time_tuple = time.localtime(timestamp)
    
            return str(time_tuple[0]) + "年" + str(time_tuple[1]) + "月" + str(time_tuple[2]) + "日"
    
        def timestamp_to_time(self, timestamp):
            if not isinstance(timestamp, (int, float)):
                return None
            locale.setlocale(locale.LC_CTYPE, 'chinese')
            time_tuple = time.localtime(timestamp)
            return str(time_tuple[3]) + "时" + str(time_tuple[4]) + "分" + str(time_tuple[5]) + "秒"
    
        def timestamp_to_datetime(self, timestamp):
            return self.timestamp_to_date(timestamp) + self.timestamp_to_time(timestamp)
    
    
    if __name__ == "__main__":
        t = TimeUtil()
        print(t.get_timestemp())
        print(t.get_date())
        print(t.get_time())
        print(t.get_datetime())
        print(t.get_chinesedate())
        print(t.get_chinesetime())
        print(t.get_chinesedatetime())
        print(t.compute_date(10))
        print(t.compute_date(-10))
        print(t.timestamp_to_date(1333333333))
        print(t.timestamp_to_time(1333333333))
        print(t.timestamp_to_datetime(1333333333))
    

      打印效果

     

  • 相关阅读:
    rdp远程Windows10连接不上的解决方案
    win10系统RuntimeBroker.exe进程占用大量cpu的解决方案
    win10磁盘管理中的“可用压缩空间大小”太小的解决方案
    修改windows10的默认字体为新宋体(并且容易区分小写l和数字1)
    WPS表格自动生成序号(不受增删影响)
    服务器CPU中的E3、E5的区别,及V2、V3、V5的区别
    屏蔽WPS广告
    解析腾讯视频真实地址
    qlv to mp4
    uefi + gpt 安装 Windows7(用Rufus制作U盘启动工具)
  • 原文地址:https://www.cnblogs.com/chongyou/p/12006193.html
Copyright © 2011-2022 走看看