日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

[轉(zhuǎn)] Python之time模塊的時間戳、時間字符串格式化與轉(zhuǎn)換

系統(tǒng) 1697 0

from?https://blog.csdn.net/mighty13/article/details/78147357

Python處理時間和時間戳的內(nèi)置模塊就有time,和datetime兩個,本文先說time模塊。

關(guān)于時間戳的幾個概念
時間戳,根據(jù)1970年1月1日00:00:00開始按秒計算的偏移量。
時間元組(struct_time),包含9個元素。

time.struct_time(tm_year=2017, tm_mon=10, tm_mday=1, tm_hour=14, tm_min=21, tm_sec=57, tm_wday=6, tm_yday=274, tm_isdst=0)
時間格式字符串,字符串形式的時間。
time模塊與時間戳和時間相關(guān)的重要函數(shù)
time.time() 生成當(dāng)前的時間戳,格式為10位整數(shù)的浮點數(shù)。
time.strftime()根據(jù)時間元組生成時間格式化字符串。
time.strptime()根據(jù)時間格式化字符串生成時間元組。time.strptime()與time.strftime()為互操作。
time.localtime()根據(jù)時間戳生成當(dāng)前時區(qū)的時間元組。
time.mktime()根據(jù)時間元組生成時間戳。
示例
關(guān)于時間戳和格式化字符串的簡單示例如下

import time

#生成當(dāng)前時間的時間戳,只有一個參數(shù)即時間戳的位數(shù),默認為10位,輸入位數(shù)即生成相應(yīng)位數(shù)的時間戳,比如可以生成常用的13位時間戳
def now_to_timestamp(digits = 10):
time_stamp = time.time()
digits = 10 ** (digits -10)
time_stamp = int(round(time_stamp*digits))
return time_stamp

#將時間戳規(guī)范為10位時間戳
def timestamp_to_timestamp10(time_stamp):
time_stamp = int (time_stamp* (10 ** (10-len(str(time_stamp)))))
return time_stamp

#將當(dāng)前時間轉(zhuǎn)換為時間字符串,默認為2017-10-01 13:37:04格式
def now_to_date(format_string="%Y-%m-%d %H:%M:%S"):
time_stamp = int(time.time())
time_array = time.localtime(time_stamp)
str_date = time.strftime(format_string, time_array)
return str_date

#將10位時間戳轉(zhuǎn)換為時間字符串,默認為2017-10-01 13:37:04格式
def timestamp_to_date(time_stamp, format_string="%Y-%m-%d %H:%M:%S"):
time_array = time.localtime(time_stamp)
str_date = time.strftime(format_string, time_array)
return str_date

#將時間字符串轉(zhuǎn)換為10位時間戳,時間字符串默認為2017-10-01 13:37:04格式
def date_to_timestamp(date, format_string="%Y-%m-%d %H:%M:%S"):
time_array = time.strptime(date, format_string)
time_stamp = int(time.mktime(time_array))
return time_stamp

#不同時間格式字符串的轉(zhuǎn)換
def date_style_transfomation(date, format_string1="%Y-%m-%d %H:%M:%S",format_string2="%Y-%m-%d %H-%M-%S"):
time_array = time.strptime(date, format_string1)
str_date = time.strftime(format_string2, time_array)
return str_date


實驗

print(now_to_date())
print(timestamp_to_date(1506816572))
print(date_to_timestamp('2017-10-01 08:09:32'))
print(timestamp_to_timestamp10(1506816572546))
print(date_style_transfomation('2017-10-01 08:09:32'))

結(jié)果為

1506836224000
2017-10-01 13:37:04
2017-10-01 08:09:32
1506816572
1506816572
2017-10-01 08-09-32
————————————————
版權(quán)聲明:本文為CSDN博主「mighty13」的原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/mighty13/article/details/78147357


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 海口市| 栖霞市| 潼南县| 北宁市| 乌苏市| 闽清县| 永嘉县| 始兴县| 沧州市| 怀柔区| 格尔木市| 遵义市| 漳浦县| 乌兰察布市| 黄平县| 安远县| 乳源| 洞口县| 麦盖提县| 三门县| 昭平县| 阿拉善盟| 台山市| 丹棱县| 乌什县| 辉县市| 海安县| 永昌县| 当涂县| 无棣县| 揭东县| 文成县| 无锡市| 商河县| 临朐县| 庆城县| 会理县| 保靖县| 嘉定区| 温宿县| 商城县|