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"))

  

python 日期封装的更多相关文章

  1. 将Python脚本封装成exe可执行文件 转

    将Python脚本封装成exe可执行文件 http://www.cnblogs.com/renzo/archive/2012/01/01/2309260.html  cx_freeze是用来将 Pyt ...

  2. Python 日期时间处理模块学习笔记

    来自:标点符的<Python 日期时间处理模块学习笔记> Python的时间处理模块在日常的使用中用的不是非常的多,但是使用的时候基本上都是要查资料,还是有些麻烦的,梳理下,便于以后方便的 ...

  3. 练习十六:Python日期格式应用(datetime)

    练习:关于python日期格式应用练习.用python方法如何输出指定格式形式的日期 这里用到datetime模块,datetime模块重新封装了time模块,提供了更多接口,提供的类包括:date, ...

  4. Python日期时间函数处理

    所有日期.时间的 api 都在datetime模块内. 1 日期的格式化输出 datetime => string import datetime now = datetime.datetime ...

  5. Python 日期和时间(转)

    Python 日期和时间 Python程序能用很多方式处理日期和时间.转换日期格式是一个常见的例行琐事.Python有一个 time 和 calendar 模组可以帮忙. 什么是Tick? 时间间隔是 ...

  6. (转)Python 日期和时间

    转自http://www.runoob.com/python/python-date-time.html Python 日期和时间 Python 程序能用很多方式处理日期和时间,转换日期格式是一个常见 ...

  7. python继承——封装

    python继承--封装 1 为什么要封装 封装数据的主要原因是:保护隐私 封装方法的主要原因是:隔离复杂度 2 封装分为两个层面 第一个层面的封装(什么都不用做):创建类和对象会分别创建二者的名称空 ...

  8. 【转载】Python日期时间模块datetime详解与Python 日期时间的比较,计算实例代码

    本文转载自脚本之家,源网址为:https://www.jb51.net/article/147429.htm 一.Python中日期时间模块datetime介绍 (一).datetime模块中包含如下 ...

  9. 【转】Python基础-封装与扩展、静态方法和类方法

    [转]Python基础-封装与扩展.静态方法和类方法 一.封装与扩展 封装在于明确区分内外,使得类实现者可以修改封装内的东西而不影响外部调用者的代码:而外部使用者只知道一个接口(函数),只要接口(函数 ...

随机推荐

  1. hypermesh中怎么设置支反力(反作用力)

    Analysis page >> Control cards >> Global output request 勾选 SPCF 和 GPFORCE .

  2. fedora23安装php,mysql

    httpd: 他的服务器根: ServerRoot, 是在/etc/httpd. 因为httpd所有的配置文件, 运行文件等都在这里.所以这是他的根. httpd的配置文件: httpd.conf恰好 ...

  3. python 每周作业

    day2:python的简介与认识day2:# 1.写一个登录程序# username# passwd# 让用户输入账号和密码,输入用户和密码输入正确的话# 提示你 xxx,欢迎登录,今天的日期是xx ...

  4. Git - 版本回溯

    在git push的时候,有时候我们会想办法撤销git commit的内容 1.找到之前提交的git commit的id git log 找到想要撤销的id 2.git reset –hard id  ...

  5. 【ABAP系列】SAP abap dialog screen屏幕参数简介

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP abap dialog ...

  6. logstash异常

    logstash异常 1 2 3 Unrecognized VM option 'UseParNewGC' Error: Could not create the Java Virtual Machi ...

  7. Html mate标签的常见功能

    一.常用的功能 1.禁止屏幕缩放 <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, us ...

  8. FormData模拟表单上传图片

    [node]---multer模块实现图片上传---FORMDATA   1.安装muterl第三方模块 cnpm install multer --save 2.使用 multer在解析完成后,会向 ...

  9. Linux下杀进程

    $ ps -ef | grep firefox smx : ? :: /usr/lib/firefox-/firefox-bin smx : pts/ :: grep --color=auto fir ...

  10. AngularJs——基础小知识(二)

    AngularJs的过滤器 1.Currency :过滤器(金额货币格式化)