python如何轉(zhuǎn)換時(shí)間戳到"2017年6月12日 18點(diǎn)24分"這樣的格式呢?
問(wèn)題描述
python如何轉(zhuǎn)換時(shí)間戳到'2017年6月12日 18點(diǎn)24分'這樣的格式呢?謝謝
import timetimestamp = time.time()time.strftime(’%Y年%m月%d日 %H時(shí)%M分’, time.localtime(timestamp))Traceback (most recent call last): File '<input>', line 1, in <module>UnicodeEncodeError: ’locale’ codec can’t encode character ’u5e74’ in position 2: Illegal byte sequence
另外吐槽下,chrome下打開segmentfault顯示內(nèi)存用完崩潰的錯(cuò)誤到底什么時(shí)候修啊啊啊啊啊啊!
問(wèn)題解答
回答1:# coding: utf8import timetimestamp = time.time() - 3600 # 時(shí)間戳print(time.strftime(’%Y{y}%m{m}%dqcy8sy8k88 %H{H}%M{M}’, time.localtime(timestamp)).format(y=’年’, m=’月’, d=’日’, H=’時(shí)’, M=’分’))回答2:
還真被我研究出來(lái)種萬(wàn)能方法,據(jù)我百度Google貌似這還是世上獨(dú)一份?
話不多說(shuō),下面上解決原理及方案:
官方文檔:https://docs.python.org/3/lib...
class time.`struct_time`?
The type of the time value sequence returned by gmtime(), localtime(), and strptime(). It is an object with a named tuple interface: values can be accessed by index and by attribute name. The following values are present:
IndexAttributeValues0tm_year(for example, 1993)1tm_monrange [1, 12]2tm_mdayrange [1, 31]3tm_hourrange [0, 23]4tm_minrange [0, 59]5tm_secrange [0, 61]; see (2) in strftime() description6tm_wdayrange [0, 6], Monday is 07tm_ydayrange [1, 366]8tm_isdst0, 1 or -1; see belowN/Atm_zoneabbreviation of timezone nameN/Atm_gmtoffoffset east of UTC in secondsNote that unlike the C structure, the month value is a range of [1, 12], not [0, 11].In calls to mktime(), tm_isdst may be set to 1 when daylight savings time is in effect, and 0 when it is not. A value of -1 indicates that this is not known, and will usually result in the correct state being filled in.When a tuple with an incorrect length is passed to a function expecting a struct_time, or having elements of the wrong type, a TypeError is raised.
看文檔可以得知time.localtime()返回的元組結(jié)構(gòu),我要用到的是年月日時(shí)分,前5個(gè),于是乎,代碼:
import timeprint(’%s年%s月%s日 %s時(shí)%s分’ % time.localtime(1497254119.69407)[:5])
輸出:
2017年6月12日 15時(shí)55分
搞定。
轉(zhuǎn)載請(qǐng)注明出處,謝謝。
相關(guān)文章:
1. mysql - 數(shù)據(jù)庫(kù)建字段,默認(rèn)值空和empty string有什么區(qū)別 1102. 新人求教MySQL關(guān)于判斷后拼接條件進(jìn)行查詢的sql語(yǔ)句3. 在mybatis使用mysql的ON DUPLICATE KEY UPDATE語(yǔ)法實(shí)現(xiàn)存在即更新應(yīng)該使用哪個(gè)標(biāo)簽?4. mysql - 這種分級(jí)一對(duì)多,且分級(jí)不平衡的模型該怎么設(shè)計(jì)表?5. Navicat for mysql 中以json格式儲(chǔ)存的數(shù)據(jù)存在大量反斜杠,如何去除?6. mysql - 數(shù)據(jù)庫(kù)表中,兩個(gè)表互為外鍵參考如何解決7. php - 公眾號(hào)文章底部的小程序二維碼如何統(tǒng)計(jì)?8. mysql - 表名稱前綴到底有啥用?9. mysql - 千萬(wàn)數(shù)據(jù) 分頁(yè),當(dāng)偏移量 原來(lái)越大時(shí),怎么優(yōu)化速度10. mac OSX10.12.4 (16E195)下Mysql 5.7.18找不到配置文件my.cnf
