Python處理時間戳和時間計算等得腳本分享

由于實際需要,簡要寫了個小,并打包生成exe,供無網絡環境下使用

1:顯示當前時間與時間戳,以及10分鐘后得時間與時間戳

# -*- coding: utf-8 -*- """Project: pyWorkspaceCreator: Administrator -haochuangCreate time: 2021-05-12 09:24IDE: PyCharmIntroduction:"""
import timeimport datetimet=datetime.datetime.now()#當前日期t1 =t.strftime('%Y-%m-%d %H:%M:%S')#轉為秒級時間戳ts1=time.mktime(time.strptime(t1, '%Y-%m-%d %H:%M:%S'))#轉為毫秒級end_time=int(str(ts1*1000).split(".")[0])#10分鐘后t2 = (t+datetime.timedelta(minutes=10)).strftime("%Y-%m-%d %H:%M:%S")# t2=(t-datetime.timedelta(hours=1)).strftime("%Y-%m-%d %H:%M:%S")#轉為秒級時間戳ts2=time.mktime(time.strptime(t2, '%Y-%m-%d %H:%M:%S'))#轉為毫秒級start_time=int(str(ts2*1000).split(".")[0])#print("n","*"*30)print("n")print("*"*30)print("當前時間戳:")print(start_time)print("當前時間:")print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ts2)))print("*"*30,"n")print("10分鐘后得時間戳:")print(end_time)print("10分鐘后得時間:")print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ts1)))print("*"*30,"n")

2:顯示當前時間與時間戳,以及10分鐘后得時間與時間戳,允許根據輸入得指定時間,生成多久之后得時間戳

# -*- coding: utf-8 -*- """Project: pyWorkspaceCreator: Administrator -haochuangCreate time: 2021-05-12 09:24IDE: PyCharmIntroduction:"""
import timeimport datetimet=datetime.datetime.now()#當前日期t1 =t.strftime('%Y-%m-%d %H:%M:%S')#轉為秒級時間戳ts1=time.mktime(time.strptime(t1, '%Y-%m-%d %H:%M:%S'))#轉為毫秒級end_time=int(str(ts1*1000).split(".")[0])#10分鐘后t2 = (t+datetime.timedelta(minutes=10)).strftime("%Y-%m-%d %H:%M:%S")# t2=(t-datetime.timedelta(hours=1)).strftime("%Y-%m-%d %H:%M:%S")#轉為秒級時間戳ts2=time.mktime(time.strptime(t2, '%Y-%m-%d %H:%M:%S'))#轉為毫秒級start_time=int(str(ts2*1000).split(".")[0])#print("n","*"*30)print("n")print("*"*30)print("當前時間戳:")print(start_time)print("當前時間:")print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ts2)))print("*"*30,"n")# 10分鐘后得時間戳print("10 分鐘后得時間戳:")print(end_time)print("10 分鐘后得時間:")print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ts1)))print("*"*30,"n")# 用戶自定義時間time_user = input("需要多少分鐘后得時間戳(請輸入正確int類型數值):")t3 = (t+datetime.timedelta(minutes=int(time_user))).strftime("%Y-%m-%d %H:%M:%S")ts3=time.mktime(time.strptime(t3, '%Y-%m-%d %H:%M:%S'))#轉為毫秒級start_time=int(str(ts3*1000).split(".")[0])print(time_user + " 分鐘后得時間戳:")print(end_time)print(time_user + " 分鐘后得時間:")print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ts3)))print("*"*30,"n")

3:顯示部分時間與時間戳等

# -*- coding: utf-8 -*- """Project: pyWorkspaceCreator: Administrator -haochuangCreate time: 2021-05-12 09:24IDE: PyCharmIntroduction:"""import timeimport datetimefrom datetime import timezonefrom datetime import timedelta# 顯示當前秒級時間戳與毫秒級時間戳、微秒級時間戳t = time.time()#print(t)  # 原始時間數據#print(int(t))  # 秒級時間戳#print(int(round(t * 1000)))  # 毫秒級時間戳#print(int(round(t * 1000000)))  # 微秒級時間戳# 顯示當前日期:dt = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')dt_ms = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f') # 含微秒得日期時間,來源 比特量化print("當前日期(s):     " + dt)print("當前日期(ms):    " + dt_ms)# 將日期轉為秒級時間戳#dtt = '2018-01-01 10:40:30'#dtts = int(time.mktime(time.strptime(dt, "%Y-%m-%d %H:%M:%S")))#ts_ms = int(time.mktime(time.strptime(dt, "%Y-%m-%d %H:%M:%S")))t=datetime.datetime.now()print("當前時間戳(s):    " + t)print("當前時間戳(ms):   " + (int(round(t * 1000))))# 國際標準時間print("國際標準時間:    "+time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime()))# 本地時間print("本地當前時間:    "+time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))# 將當前日期轉為秒級時間戳dt = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())dt_ts = int(time.mktime(time.strptime(dt, "%Y-%m-%d %H:%M:%S")))print("當前時間:        " + dt)print("當前時間戳:      " + dt_ts)# 將獲取十分鐘后得秒級時間戳#dt_10 = int((datetime.datetime.now()+datetime.timedelta(minutes=10)).strftime("%Y-%m-%d %H:%M:%S"))#ts_10 = int(time.mktime(time.strptime(dt_10, "%Y-%m-%d %H:%M:%S")))after10 = (datetime.datetime.now()+datetime.timedelta(minutes=10)).strftime("%Y-%m-%d %H:%M:%S")after10_ts = int(time.mktime(time.strptime(t1,after10)))print("10分鐘后得時間:   " + after10)print("10分鐘后得時間戳: "

4:顯示部分時間與時間戳等

# -*- coding: utf-8 -*- """Project: pyWorkspaceCreator: Administrator -haochuangCreate time: 2021-05-12 09:08IDE: PyCharmIntroduction:"""import datetimeimport timeprint('*'*30 +"獲取時間方式")#獲取當前時間:Thu Nov 03 16:40:00 2016print(time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()))#獲取當前時間:2016-11-03 16:40:00print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))#獲取年,月,日:2016-11-03print(datetime.date.today())#獲取當前時間:2016-11-03 16:43:14.550000print(datetime.datetime.now())#不加參數是00:00,參數days=1表示一天:1 day, 0:00:00print(datetime.timedelta(days=1))#獲取昨天日期:2016-11-02nowtime=datetime.date.today()oldtime=datetime.timedelta(days=1)print(nowtime-oldtime)#獲取昨天得精確日期oldtime=datetime.timedelta(days=1)print (datetime.datetime.now() - oldtime)print ('*'*30 + 'python時間處理之time模塊')import time# 返回時間戳# print(time.time())# 返回當前時間print(time.ctime())# 返回一天前得時間print(time.ctime(time.time()-86400))# 函數返回time.struct_time類型得對象time_obj = time.gmtime()print(time_obj)#結果:time.struct_time(tm_year=2016, tm_mon=7, tm_mday=27, tm_hour=8, tm_min=52, tm_sec=26, tm_wday=2, tm_yday=209, tm_isdst=0)# 格式化輸出:print(time_obj.tm_year,time_obj.tm_mon,time_obj.tm_mday)print("{year}-{month}".format(year=time_obj.tm_year,month=time_obj.tm_mon))# 以time.struct_time類型,打印本地時間print(time.localtime())# 轉換成時間戳time_obj = time.gmtime()print(time.mktime(time_obj))# 延時2秒time.sleep(2)# 打印UTC,世界標準時間,北京時區是東八區,領先UTC八個小時print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()))# 本地時間print(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()))# 把time.struct_time類型時間,轉換成時間戳tm = time.strptime("2016-05-6 15:06:33","%Y-%m-%d %H:%M:%S")print(tm)print(time.mktime(tm))print ('*'*30 + '3-python時間處理之datetime模塊')import datetime# 打印當前,年,月,日print(datetime.date.today())# 打印當前時間,精確到微秒current_time = datetime.datetime.now()print(current_time)# 轉成time.struct_time格式時間current_time = datetime.datetime.now()print(current_time.timetuple())# 加十天print(datetime.datetime.now() +datetime.timedelta(days=10))# 減十天print(datetime.datetime.now() +datetime.timedelta(days=-10))# 減十個小時print(datetime.datetime.now() +datetime.timedelta(hours=-10))# 加120sprint(datetime.datetime.now() +datetime.timedelta(seconds=120))# 替換成指定得時間cr_time = datetime.datetime.now()print(cr_time.replace(2014,9,12))# 結果:2014-09-12 17:28:17.522893# 格式化輸出print(datetime.datetime.strptime("21/11/06 16:30","%d/%m/%y %H:%M"))# 替換成指定時間后,類型是<class 'datetime.datetime'>current_time = datetime.datetime.now()time_obj = current_time.replace(2015,5)print(time_obj,type(time_obj))# 結果:2015-05-27 17:34:13.350245 <class 'datetime.datetime'># 對比時間大小,取指定時間范圍使用current_time = datetime.datetime.now()time_obj = current_time.replace(2015,5)print(current_time>time_obj)import datetimedef getYesterday():    today=datetime.date.today()    oneday=datetime.timedelta(days=1)    yesterday=today-oneday    return yesterday# 輸出print(getYesterday())

5:關于時間戳處理

# -*- coding: utf-8 -*- """Project: pyWorkspaceCreator: Administrator -haochuangCreate time: 2021-05-12 09:24IDE: PyCharmIntroduction:"""import timeimport datetimefrom datetime import timezonefrom datetime import timedelta# 顯示當前秒級時間戳與毫秒級時間戳、微秒級時間戳t = time.time()print(t)  # 原始時間數據print(int(t))  # 秒級時間戳print(int(round(t * 1000)))  # 毫秒級時間戳print(int(round(t * 1000000)))  # 微秒級時間戳# 顯示當前日期:dt = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')dt_ms = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f') # 含微秒得日期時間,來源 比特量化print(dt)print(dt_ms)# 將日期轉為秒級時間戳dt = '2018-01-01 10:40:30'ts = int(time.mktime(time.strptime(dt, "%Y-%m-%d %H:%M:%S")))print(ts)# 將秒級時間戳轉為日期ts = 1515774430dt = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ts))print(dt)# 時區轉換# 顯示UTC時間utc_now = datetime.datetime.utcnow()print(utc_now)# 世界標準時間# utc_time = datetime(2019, 7, 30, 7, 50, 0)print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))# 北京時間UTC+8# cst_time =utc_time.astimezone(timezone(timedelta(hours=-8))).strftime("%Y-%m-%d %H:%M:%S")# 國際標準時間print("國際標準時間:"+time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime()))# 本地時間print("本地時間:"+time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))

到此這篇關于Python處理時間戳和時間計算等得分享得內容就介紹到這了,更多相關Python 時間戳 時間計算內容請搜索之家以前得內容或繼續瀏覽下面得相關內容希望大家以后多多支持之家!

聲明:所有內容來自互聯網搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。
發表評論
更多 網友評論1 條評論)
暫無評論

返回頂部

主站蜘蛛池模板: 国产精品久久久久鬼色| 在线天堂bt种子| 亚洲成a人片在线观看www| 麻绳紧缚奴隷女囚| 怡红院亚洲色图| 亚洲天堂岛国片| 色噜噜狠狠一区二区三区| 在线成人a毛片免费播放| 久久精品天天中文字幕人妻 | 日本边添边摸边做边爱边视频| 北条麻妃在线一区二区| 1024在线观看国产天堂| 无码av岛国片在线播放| 亚洲熟妇少妇任你躁在线观看无码 | mhsy8888| 日韩精品中文乱码在线观看| 免费超爽大片黄| 很黄很黄的网站免费的| 强开小娟嫩苞又嫩又紧| 亚洲免费网站在线观看| 美村妇真湿夹得我好爽| 国产精品永久久久久久久久久| 久99久热只有精品国产女同| 欧美特黄三级在线观看| 国产一卡二卡三卡| 18美女腿打开无遮挡| 成人区人妻精品一区二区不卡| 亚洲偷自精品三十六区| 精品国产综合区久久久久久| 国产真实乱人视频| taoju.tv| 日本免费www| 亚洲尹人九九大色香蕉网站| 羞羞视频在线观看网站| 国产精品91av| japanese六十路| 日本中文字幕有码在线视频| 亚洲国产精品久久丫| 精品哟哟哟国产在线不卡| 日本大片免a费观看视频| 亚洲精品国产综合久久一线|