python 日期封裝

2022-06-04 08:45:13 字數 3130 閱讀 2792

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

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

日期類的封裝

include using namespace std class date 拷貝建構函式 date const date d 析構函式 date 判斷是不是閏年,四年一閏,百年不閏,四百年一閏 bool isleapyear int year else 判斷是不是合法日期,小於當前年月的日期或大於...

常用日期封裝方法

目錄 判斷時間是否在時間段內 判斷時間是否在時間段內 param nowtime 需要判斷的時間 param begintime param endtime return public static boolean belongcalendar date nowtime,date begintime...

JS 日期封裝操作

js對日期封裝使用,採用正則匹配日期結構進行顯示日期。字串轉化為日期物件 呼叫格式為 str.format2date yyyy mm dd hh mm ss param 傳入特殊格式 return string.prototype.format2date function style,option ...