欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python实现简单日期工具类

程序员文章站 2022-06-30 09:48:57
本文实例为大家分享了python实现简单日期工具类的具体代码,供大家参考,具体内容如下 import datetime import time datet...

本文实例为大家分享了python实现简单日期工具类的具体代码,供大家参考,具体内容如下

import datetime
import time

datetime_format = "%y-%m-%d %h:%m:%s"
time_format = "%h:%m:%s"

#当前毫秒数
def curmilis():
  return int(time.time() * 1000)

#当前秒数
def curseconds():
  return int(time.time())

#当前日期 格式%y-%m-%d %h:%m:%s
def curdatetime():
  return datetime.datetime.strftime(datetime.datetime.now(),datetime_format)

#当前日期 格式%y-%m-%d
def curdate():
  return datetime.date.today()

#当前时间 格式%y-%m-%d
def curtime():
  return time.strftime(time_format)

#秒转日期
def secondstodatetime(seconds):
  return time.strftime(datetime_format,time.localtime(seconds))

#毫秒转日期
def milistodatetime(milix):
  return time.strftime(datetime_format,time.localtime(milix//1000))

#日期转毫秒
def datetimetomilis(datetimestr):
  strf = time.strptime(datetimestr,datetime_format)
  return int(time.mktime(strf)) * 1000

#日期转秒
def datetimetoseconds(datetimestr):
  strf = time.strptime(datetimestr,datetime_format)
  return int(time.mktime(strf))

#当前年
def curyear():
  return datetime.datetime.now().year
#当前月
def curmonth():
  return datetime.datetime.now().month

#当前日
def curday():
  return datetime.datetime.now().day

#当前时
def curhour():
  return datetime.datetime.now().hour

#当前分
def curminute():
  return datetime.datetime.now().minute

#当前秒
def cursecond():
  return datetime.datetime.now().second

#星期几
def curweek():
  return datetime.datetime.now().weekday()

#几天前的时间
def nowdaysago(days):
  daysagotime = datetime.datetime.now() - datetime.timedelta(days = days)
  return time.strftime(datetime_format,daysagotime.timetuple())

#几天后的时间
def nowdaysafter(days):
  daysagotime = datetime.datetime.now() + datetime.timedelta(days = days)
  return time.strftime(datetime_format,daysagotime.timetuple())

#某个日期几天前的时间
def dtimedaysago(dtimestr,days):
  daysagotime = datetime.datetime.strptime(dtimestr,datetime_format) - datetime.timedelta(days = days)
  return time.strftime(datetime_format,daysagotime.timetuple())

#某个日期几天前的时间
def dtimedaysafter(dtimestr,days):
  daysagotime = datetime.datetime.strptime(dtimestr,datetime_format) + datetime.timedelta(days = days)
  return time.strftime(datetime_format,daysagotime.timetuple())


secondstamp = curseconds()
print("当前秒:",secondstamp)
milisstamp = curmilis()
print("当前毫秒:",milisstamp)

curdtime = curdatetime()
print("当前时间:",curdtime)
curdate = curdate()
print("当前日期:",curdate)
curt = curtime()
print("当前时刻:",curt)


stdtime = secondstodatetime(secondstamp)
print("秒转时间:",stdtime)
mtdtime = milistodatetime(milisstamp)
print("毫秒转时间:",mtdtime)
dtimetm = datetimetomilis(mtdtime)
print("时间转毫秒:",dtimetm)
dtimets = datetimetoseconds(mtdtime)
print("时间转秒:",dtimets)

year = curyear()
print("年:",year)
month = curmonth()
print("月:",month)
day = curday()
print("日:",day)
hour = curhour()
print("时:",hour)
minute = curminute()
print("分:",minute)
second = cursecond()
print("秒:",second)
week = curweek()
print("星期:",week)

输出结果如下:

当前秒: 1518341913
当前毫秒: 1518341913403
当前时间: 2018-02-11 17:38:33
当前日期: 2018-02-11
当前时刻: 17:38:33
秒转时间: 2018-02-11 17:38:33
毫秒转时间: 2018-02-11 17:38:33
时间转毫秒: 1518341913000
时间转秒: 1518341913
年: 2018
月: 2
日: 11
时: 17
分: 38
秒: 33
星期: 6
[finished in 0.2s]

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。