search

Home  >  Q&A  >  body text

How does python convert the timestamp to the format of "June 12, 2017 18:24"?

How does python convert the timestamp to the format of "June 12, 2017 18:24"? Thanks

import time
timestamp = 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

Another complaint, when opening segmentfault in chrome will show the error of running out of memory and crashing, when will it be fixed?

欧阳克欧阳克2757 days ago1164

reply all(2)I'll reply

  • 漂亮男人

    漂亮男人2017-06-13 09:26:50

    # coding: utf8
    import time
    timestamp = 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='分'))
    
    

    reply
    0
  • 女神的闺蜜爱上我

    女神的闺蜜爱上我2017-06-13 09:26:50

    I have really researched a universal method. According to my Baidu Google, it seems that this is the only one in the world?

    Without further ado, here are the solution principles and solutions:

    Official documentation: 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:

    Index Attribute Values
    0 tm_year (for example, 1993)
    1 tm_mon range [1, 12]
    2 tm_mday range [1, 31]
    3 tm_hour range [0, 23]
    4 tm_min range [0, 59]
    5 tm_sec range [0, 61]; see (2) in strftime() description
    6 tm_wday range [0, 6], Monday is 0
    7 tm_yday range [1, 366]
    8 tm_isdst 0, 1 or -1; see below
    N/A tm_zone abbreviation of timezone name
    N/A tm_gmtoff offset east of UTC in seconds

    Note 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.


    Looking at the documentation, you can see the tuple structure returned by time.localtime(). What I want to use is the year, month, day, hour, and the first 5, so here’s the code:

    import time
    print('%s年%s月%s日 %s时%s分' % time.localtime(1497254119.69407)[:5])

    Output:

    2017年6月12日 15时55分

    Done.

    Please indicate the source when reprinting, thank you.

    reply
    0
  • Cancelreply