簡單的日期時間應用

2021-10-25 15:22:31 字數 4621 閱讀 5181

封裝了一些時間日期,方便後邊用到

#encoding=utf-8

import time

def get_current_year(): #獲取當前時間年

return str(time.localtime().tm_year) + '年'

def get_current_month(): #獲取當前時間月

return str(time.localtime().tm_mon) + '月'

def get_current_day(): #獲取當前時間日

return str(time.localtime().tm_mday) + '日'

def get_current_hour(): #獲取當前時間時

return str(time.localtime().tm_hour) + '時'

def get_current_date(): #獲取當前時間年月日以反斜線分割

return time.strftime('%y/%m/%d', time.localtime())

def get_current_time(): #獲取當前時間時分秒,不分割,截圖專用

return time.strftime('%h%m%s', time.localtime())

def get_english_current_date(): #獲取當前時間年月日以中線分割

return time.strftime('%y-%m-%d', time.localtime())

def get_english_current_time(): #獲取當前時間時分秒以冒號分割

return time.strftime('%h:%m:%s', time.localtime())

def get_chinese_current_date(): #獲取中文當前時間年月日

year = str(time.localtime().tm_year)

if time.localtime().tm_mon < 10:

month = '0'+str(time.localtime().tm_mon)

else:

month = str(time.localtime().tm_mon)

if time.localtime().tm_mday < 10:

day = '0'+str(time.localtime().tm_mday)

else:

day = str(time.localtime().tm_mday)

return '%s年%s月%s日' %(year, month, day)

def get_chinese_current_time(): #獲取中文當前時間時分秒

if time.localtime().tm_hour < 10:

hour = '0'+str(time.localtime().tm_hour)

else:

hour = str(time.localtime().tm_hour)

if time.localtime().tm_min < 10:

minute = '0'+str(time.localtime().tm_min)

else:

minute = str(time.localtime().tm_min)

if time.localtime().tm_sec < 10:

sec = '0'+str(time.localtime().tm_sec)

else:

sec = str(time.localtime().tm_sec)

return '%s時%s分%s秒' %(hour, minute, sec)

def get_chinese_current_datetime(): #獲取中文當前時間年月日時分秒

return get_chinese_current_date() + ' ' + get_chinese_current_time()

def get_english_current_datetime(): #獲取當前時間年月日時分秒 中線冒號分割

return get_english_current_date() + ' ' +get_english_current_time()

def get_chinese_anytime_date(str_date): #獲取中文任何時間年月日

if not isinstance(str_date, int):

return '時間戳輸入錯誤'

year = str(time.localtime(str_date).tm_year)

if time.localtime(str_date).tm_mon < 10:

month = '0'+str(time.localtime(str_date).tm_mon)

else:

month = str(time.localtime(str_date).tm_mon)

if time.localtime(str_date).tm_mday < 10:

day = '0'+str(time.localtime(str_date).tm_mday)

else:

day = str(time.localtime(str_date).tm_mday)

return '%s年%s月%s日' %(year, month, day)

def get_chinese_anytime_time(str_time): #獲取中文任何時間時分秒

if not isinstance(str_time,int):

return '時間戳輸入錯誤'

if time.localtime(str_time).tm_hour < 10:

hour = '0'+str(time.localtime(str_time).tm_hour)

else:

hour = str(time.localtime(str_time).tm_hour)

if time.localtime(str_time).tm_min < 10:

minute = '0'+str(time.localtime(str_time).tm_min)

else:

minute = str(time.localtime(str_time).tm_min)

if time.localtime(str_time).tm_sec < 10:

sec = '0'+str(time.localtime(str_time).tm_sec)

else:

sec = str(time.localtime(str_time).tm_sec)

return '%s時%s分%s秒' %(hour, minute, sec)

def get_english_anytime_date(str_date): #獲取任何時間年月日以中線分割

if not isinstance(str_date, int):

return '時間戳輸入錯誤'

return time.strftime('%y-%m-%d', time.localtime(str_date))

def get_english_anytime_time(str_time): #獲取任何時間時分秒 冒號分割

return time.strftime('%h:%m:%s', time.localtime(str_time))

if __name__=='__main__':

print('當前時間年= ', get_current_year())

print('當前時間月= ', get_current_month())

print('當前時間日= ', get_current_day())

print('當前時間時= ', get_current_hour())

print('當前時間年月日以/分開顯示= ', get_current_date())

print('當前時間時分秒以/分割= ', get_current_time())

print('當前時間年月日以-分開顯示= ', get_english_current_date())

print('當前時間時分秒以:分開= ', get_english_current_time())

print('中文當前時間年月日= ', get_chinese_current_date())

print('中文當前時間時分秒= ', get_chinese_current_time())

print('中文當前日期時間= ', get_chinese_current_datetime())

print('當前日期時間以-:分割= ', get_english_current_datetime())

print('中文某一時間年月日= ', get_chinese_anytime_date(1242424025))

print('中文某一時間時分秒= ', get_chinese_anytime_time(43434))

print('某一時間年月日以-分開顯示= ', get_english_anytime_date(1242424575))

print('某一時間時分秒以:分開= ', get_english_anytime_time(43434))

PHP應用日期與時間

時間戳 1.是乙個整數 2.1970 1 1 到現在的秒數 1213212121 2014 02 14 11 11 11 02 14 2014 11 11 11 date default timezone set prc start microtime true for i 0 i 100000 i...

Android應用日期 選擇日期和時間

為了讓使用者能選擇日期和時間,android提供了日期 時間選擇器,分別是datepicker元件和timepicker元件。為了在程式中可以獲取使用者選擇的日期時間,還需要為datepicker元件和timepicker元件新增事件 1 在新建專案的布局檔案中,新增日期拾取器和時間拾取器。2 在主...

PHP日期時間函式的高階應用技巧

php的日期時間函式date 中介紹了php日期時間函式的簡單用法,這類將介紹更多的函式來豐富我們的應用。checkdate month,date,year 如果應用的值構成乙個有效日期,則該函式返回為真。例如,對於錯誤日期2005年2月31日,此函式返回為假。在日期用於計算或儲存在資料庫中之前,可...