import time
import datetime
import locale 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):
strTime = time.strftime("%Y年%m月%d日", time.localtime())
return strTime def get_chinesetime(self):
strTime = time.strftime("%H时%M分%S秒", time.localtime())
return strTime def get_chinesedatetime(self):
# locale.setlocale(locale.LC_CTYPE, 'chinese') # 如果是win,需要加上这个,mac 可以不使用
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
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
time_tuple = time.localtime(timestamp)
return str(time_tuple[4]) + "时" + str(time_tuple[5]) + "分" + str(time_tuple[6]) + "秒" def timestamp_to_datetime(self, timestamp):
return self.timestamp_to_date(timestamp) + self.timestamp_to_time(timestamp) def getEveryDay(self, start, end):
date_list = []
begin_date = datetime.datetime.strptime(start, "%Y-%m-%d")
end_date = datetime.datetime.strptime(end, "%Y-%m-%d")
while begin_date <= end_date:
date_str = begin_date.strftime("%Y-%m-%d")
date_list.append(date_str)
begin_date += datetime.timedelta(days=1)
print('共生成了%s天' % str(len(date_list)))
return date_list def getTime(self, t):
"""单个日期初始化时间戳"""
dt = time.strptime(t, '%Y-%m-%d %H:%M:%S')
time_stamp = int(time.mktime(dt))
return time_stamp 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))
print(t.getEveryDay("2019-06-01", "2019-07-01"))
print(t.getTime("2019-06-01 18:31:00"))

  

最新文章

  1. Docker dockerfile创建Eclipse镜像初试
  2. 使用C#代码部署SharePoint 2013开发包简单总结(一)
  3. 解决.NET Core中MailKit无法使用阿里云邮件推送服务的问题
  4. Jquery-选择框点击勾选或者取消
  5. [转] 再探java基础——break和continue的用法
  6. 20141016--for 兔子
  7. 企业应用的Web程序的安全性
  8. UVa 658 (Dijkstra) It&#39;s not a Bug, it&#39;s a Feature!
  9. console调试--转
  10. poj 1276 Cash Machine_多重背包
  11. Android SwipeRefreshLayout 官方下拉刷新控件介绍
  12. zoj1537- Playing with a Calculator
  13. Redis系列三(redis配置文件分析)
  14. [个人翻译]GitHub指导文件(GitHub Guides[Hello World])
  15. 微信小程序与AspNetCore SignalR聊天实例
  16. c# ASP.NET Core2.2利用中间件支持跨域请求
  17. 11. IDS (Intrusion detection systems 入侵检测系统 6个)
  18. hibernate session.createSQLQuery(sql); 通过命令删除
  19. flask_sqlalchemy中根据聚合分组后的结果进行排序,根据日期(datetime)按天分组
  20. 隐写工具Hydan的安装使用方法

热门文章

  1. win10 1903
  2. CentOS yum 安装历史版本 java
  3. 5个用/不用GraphQL的理由
  4. sshd使用
  5. 模拟赛DAY 2 T1江城唱晚
  6. SeaJS基本开发原则
  7. pc显示,手机隐藏
  8. 004-windows(64位)下使用curl命令
  9. Spring命名空间引入方法
  10. Delphi XE2 之 FireMonkey 入门(12) - 动画(上)