Editing
Python programming
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== HOWTO get an [https://en.wikipedia.org/wiki/ISO_8601 ISO 8601] or [https://www.obj-sys.com/asn1tutorial/node14.html ASN.1 GeneralizedTime] time format with Python datetime object === <pre>>>> import datetime >>> datetime.MINYEAR 1 >>> datetime.MAXYEAR 9999 >>> datetime.datetime.now() datetime.datetime(2019, 3, 31, 21, 29, 47, 993877) >>> from datetime import datetime >>> datetime.now() datetime.datetime(2019, 3, 31, 21, 30, 4, 999219) >>> d = datetime.now() >>> d datetime.datetime(2019, 3, 31, 21, 30, 51, 362520) >>> f = '{:%Y%m%d%H%M%S.%f}' < -- Setup a string format to control the datetime format. >>> f.format(d) '20190331213051.362520' < -- microsecond accuracy >>> d2 = datetime(2019, 3, 31, 21, 30, 51, 000000) < -- Create datetime object with exactly zero microseconds to verify string format. >>> f.format(d2) '20190331213051.000000' < -- verified that datetime displays six zeros >>> '20190331213051.000000'[:-3] < -- truncate last three digits '20190331213051.000' < -- Now we need timezone offset >>> z = datetime.datetime.utcnow() < -- I want UTC time so I use datetime class method utcnow() >>> z datetime.datetime(2019, 4, 1, 3, 28, 53, 152240) < -- not in format I want >>> f.format(z) < -- apply format specification used above '20190401032853.152240' < -- want to remove least 3 insignificant digits from microseconds >>> f.format(z)[:-3] '20190401032853.152' < -- missing offset >>> f.format(z)[:-3]+'Z' < -- simple string concatenate '20190401032853.152Z' < -- final product</pre> <big>Summary of string formatting to get to [https://en.wikipedia.org/wiki/ISO_8601 ISO 8601] from [https://docs.python.org/3/library/datetime.html#module-datetime Python datetime object]</big> * Format string: <code>'{:%Y%m%d%H%M%S.%f}'</code> * Truncate last three digits from formatted string: <code>[:-3]</code> * Concatenate 'Z' for Zulu time <code>+ 'Z'</code> <big>Complete example (without references)</big> <pre>>>> '{:%Y%m%d%H%M%S.%f}'.format(datetime.datetime.utcnow())[:-3] + 'Z' '20190401034018.177Z'</pre> Same example with f-string <pre>f'{datetime.utcnow():%Y%m%d%H%M%S.%f}'[:-3]+'Z'</pre>
Summary:
Please note that all contributions to GotOpinion may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
GotOpinion:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
Edit source
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information