python實現簡單日期工具類

2022-10-04 17:39:13 字數 3944 閱讀 1488

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 dat程式設計客棧etime.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()

#幾天前的時間

de程式設計客棧f 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)

milisstamwww.cppcns.comp = 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]syiwkykaa

本文標題: python實現簡單日期工具類

本文位址:

PHP實現簡單日曆類編寫

用php實現日曆類的編寫,供大家參考,具體內容如下 calendar.class.php 建立乙個日曆類 修改預設時區 date default timezone set prc class calendar function showcalendar 1 顯示星期 private function...

實現簡單日曆

當前年 asp label id lbyear runat server text asp label 當前月 asp label id lbmonth runat server text asp label asp button id premonth runat server text 上一月 ...

c 簡單日誌類檔案

ifndef logger h define logger h define log file logger.log enum log level enum log mode extern int fmtwrite int fd,const char fmt,將格式化的內容寫入到檔案 extern ...