python如何转换时间戳到"2017年6月12日 18点24分"这样的格式呢?
问题描述
python如何转换时间戳到'2017年6月12日 18点24分'这样的格式呢?谢谢
import timetimestamp = time.time()time.strftime(’%Y年%m月%d日 %H时%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显示内存用完崩溃的错误到底什么时候修啊啊啊啊啊啊!
问题解答
回答1:# coding: utf8import timetimestamp = time.time() - 3600 # 时间戳print(time.strftime(’%Y{y}%m{m}%d{d} %H{H}%M{M}’, time.localtime(timestamp)).format(y=’年’, m=’月’, d=’日’, H=’时’, M=’分’))回答2:
还真被我研究出来种万能方法,据我百度Google貌似这还是世上独一份?
话不多说,下面上解决原理及方案:
官方文档: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()返回的元组结构,我要用到的是年月日时分,前5个,于是乎,代码:
import timeprint(’%s年%s月%s日 %s时%s分’ % time.localtime(1497254119.69407)[:5])
输出:
2017年6月12日 15时55分
搞定。
转载请注明出处,谢谢。
相关文章:
1. 为什么span的color非要内联样式才起作用?2. 小白问题getDay()3. html5 - H5做的手机分享页微信更新后,分享出去不再默认显示第一个图 作为缩略图4. golang - 用IDE看docker源码时的小问题5. docker start -a dockername 老是卡住,什么情况?6. 手机开发 - Android蓝牙模块连接后怎么接收数据?求助7. css - div设置float:left后高度设置自动会无效 ?8. 请问一下各位老鸟 我一直在学习独孤九贱 现在是在tp5 今天发现 这个系列视频没有实战9. 求救一下,用新版的phpstudy,数据库过段时间会消失是什么情况?10. 老师,请问我打开browsersync出现这个问题怎么解决啊?

网公网安备