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. React-Native 之 GD (十二)海淘半小时热门 及 获取最新数据个数功能 (角标)

    1.海淘半小时热门   基本功能和首页相似 GDHt.js /** * 海淘折扣 */ import React, { Component } from 'react'; import { Style ...

  2. qbzt day7上午

    由于优盘咕咕咕了,所以这篇就咕咕咕了 以后还会补上的 qwq

  3. springboot定时任务出错 Unexpected use of scheduler.

    java.lang.IllegalStateException: Unexpected use of scheduler. 在启动类加: @Bean public ThreadPoolTaskSche ...

  4. Python解决ModuleNotFoundError: No module named 'Queue'的问题

    我们知道Python2和Python3两个版本之间,有些不兼容的地方,Python3中引入Queue会报出这个问题. Python3中要这样引入: import queue Python2中要这样引入 ...

  5. YOLOv3 算法的详细说明

    YOLOv3没有太多的创新,主要是借鉴一些好的方案融合到YOLO里面.不过效果还是不错的,在保持速度优势的前提下,提升了预测精度,尤其是加强了对小物体的识别能力. 本文主要讲v3的改进,由于是以v1和 ...

  6. CGI 环境变量

    CGI 环境变量 环境变量 意义 SERVER_NAME CGI脚本运行时的主机名和IP地址. SERVER_SOFTWARE 你的服务器的类型如: CERN/3.0 或 NCSA/1.3. GATE ...

  7. Java设计模式——模板方法设计模式(abstract修饰)

    解释: 一个抽象类中,有一个主方法,再定义 1...n 个方法,可以是抽象的,也可以是实际的方法,定义一个类,继承该抽象类,重写抽象方法,通过调用抽象类,实现对子类的调用. 解决的问题: 当功能内部一 ...

  8. Super Mario HDU 4417 主席树区间查询

    Super Mario HDU 4417 主席树区间查询 题意 给你n个数(编号从0开始),然后查询区间内小于k的数的个数. 解题思路 这个可以使用主席树来处理,因为这个很类似查询区间内的第k小的问题 ...

  9. SCUT - 142 - 第n个素数

    https://scut.online/p/142 但是洲阁筛打表还是超时了,打的表不够长吧,在51nod上面要跑5s.要是快10倍得要密1000倍,根本打不出来(时间意义). 暴力check要找的质 ...

  10. 树莓派Pi账户密码简单重置

    由于经常忘记树莓派Pi账户的密码而导致无法正常的玩树莓派,本篇文章综合网上的教程,总结了两种快速重置树莓派Pi账户密码的方法,以下一切操作都需在树莓派本机上进行操作. 方法一: 打开终端,执行 sud ...