简体   繁体   English

如何将python中的时间戳格式化为可读格式?

[英]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.我有一个 slack 机器人,它可以获取日程信息并打印用户日程的开始和结束时间。 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当前的时间格式是2019-06-28T15:12:49-04:00 ,但我希望它更具可读性,例如Fri Jun 28 15:12:49 2019

You can use dateparser to parse the date time string easily.您可以使用dateparser轻松解析日期时间字符串。

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:要转换任何格式的时间戳,您可以使用 datetime 模块,如下所述:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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