简体   繁体   中英

What's the best way to get a date from a list of values

Not sure how to go about it but what would be the easiest way to map the following values and return a date in output?

values = [2009, 11, 1, 2, 6, 9, 19, 0]
map = ['Year', 'Month', 'Day Of Week', 'Day', 'Hr', 'Min', 'Sec', 'MSec']
output = '2 NOV 2009 06:09:19 UTC'

EDIT:

output preferably a datetime obj and not string. Sorry for the confusion List will always be length of 8 values. and always mapped to the map index order. So map and values will always be 8 and map[0] should be values[0]

simply unpack the appropriate values from the "values" list into datetime.datetime:

from datetime import datetime, timezone

values = [2009, 11, 1, 2, 6, 9, 19, 0]

dtobj = datetime(*values[:2], *values[3:], tzinfo=timezone.utc)

output = dtobj.strftime('%d %b %Y %H:%M:%S %Z').lstrip('0')
# '2 Nov 2009 06:09:19 UTC'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM