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. HTML To Word

    一.源码特点       1.在一些实际的应用场景中,有用户需要将一批规范的资料网页保存为WORD文档以便离线传阅或用于其它需求.在此之前,使用了OFFICE组件来尝试完成此功能需求,但是效果都不尽如 ...

  2. Oracle Flashback Transaction Query with Oracle Flashback Version Query

    Oracle Flashback Transaction Query with Oracle Flashback Version Query In this example, a database a ...

  3. C# 防火墙操作之特定程序

    将特定程序加入防火墙组,与将特定端口加入防火墙流程类似.详情见“C# 防火墙操作之特定端口”.其主要代码为: /// <summary> /// 允许应用程序通过防火墙 /// </ ...

  4. 001-spring boot概述与课程概要

    一.Spring Boot介绍 Spring Boot的目的在于创建和启动新的基于spring框架的项目.Spring boot会选择最适合的Spring 子项目和第三方开源库进行整合.大部分Spri ...

  5. UI自动化之特殊处理二(弹框\下拉框\选项\文件上传)

    弹框\下拉框\选项\文件上传也是一些比较特殊的操作 目录 1.弹框 2.下拉框 3.选项 4.文件上传 1.弹框 弹框有三种形式,value为alert.confirm.prompt三种的弹框,第一个 ...

  6. beego 注解路由无效问题分析

    问题描述:学习 beego 框架发现注解路由无效,除了不能找到路由外,未见任何异常. 问题解决:将配置文件中的 runmode 更改为 dev 模式. 问题分析: 如果没有设置过 runmode 不会 ...

  7. assert 与if

    strlen的实现用不用加断言(assert)? http://en.cppreference.com/w/cpp/error/assert 自己写strlen实现会加assert判断空指针,Debu ...

  8. pyenv python 版本控制

    经常遇到这样的情况: 系统自带的Python是2.x,自己需要Python 3.x,此时需要在系统中安装多个Python,但又不能影响系统自带的Python,即需要实现Python的多版本共存,pye ...

  9. vscode怎么修改颜色主题里的某种颜色

    我习惯用深色主题, 齿轮--->颜色主题 ---->monokai是我的菜. 比较精神,又不刺眼. 但是这个主题的注释的颜色太浅了. 几乎和背景重合. 注释很重要, 能体现和记录自己的代码 ...

  10. 【洛谷p1309】瑞士轮

    因为太菜不会写P1310 表达式的值,就只能过来水两篇博客啦qwq 另外这个题我是开o2才过的(虽然是写了归并排序)(可能我太菜写的归并不是还可以“剪枝”吧qwq)哎,果真还是太菜啦qwq 所以准备写 ...