简体   繁体   中英

How to format a timestamp in python to a readable format?

I have a slack bot that gets schedule information and prints the start and end times of the user's schedule. I am trying to format the timestamp so that it is a more readable format. Is there a way to format this response?

Here is an example of the output:

co = get_co(command.split(' ')[1])
start = get_schedule(co['group'])['schedule']['schedule_layers'][0]['start']
end = get_schedule(co['group'])['schedule']['schedule_layers'][0]['end']
response = 'Start: {} \n End: {}'.format(start,end)

The current time format is 2019-06-28T15:12:49-04:00 , but I want it to be something more readable like Fri Jun 28 15:12:49 2019

You can use dateparser to parse the date time string easily.

import dateparser

date = dateparser.parse('2019-06-28T15:12:49-04:00') # parses the date time string

print(date.strftime('%a %b %d %H:%m:%M %Y'))
# Fri Jun 28 15:06:12 2019

See this in action here

To convert timestamps of any format you can use the datetime module as described here:

https://stackabuse.com/converting-strings-to-datetime-in-python/

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