[英]How can I Convert Normal timestamp to Epoch Timestamp in python
Question 1: Convert the human readable date to epoch timestamp/time as on the start of the day?问题 1:将人类可读的日期转换为一天开始时的纪元时间戳/时间? Example.
例子。 If the INPUT date is 21-01-2020, print the epoch time at the start of the day ie 12:00 AM Conditions: Date limit: 01-01-1970 to Input Description: The input date will be in any of the following format.
如果 INPUT 日期是 21-01-2020,则在一天开始时打印纪元时间,即 12:00 AM 条件:日期限制:01-01-1970 到 输入说明:输入日期将是以下任何一种格式。
Output Description: For all the types of above input date, the output should be an Epoch timestamp/time. Output 描述:对于上述所有类型的输入日期,output 应该是一个 Epoch 时间戳/时间。
Question 1 – Date to Epoch timestamp问题 1 – 日期到 Epoch 时间戳
Exceptions: Any input date other than the given formats must be handled and a message "Unable to convert the provided date" must be printed.例外:必须处理给定格式以外的任何输入日期,并且必须打印消息“无法转换提供的日期”。
More Examples:更多示例:
Example 1 Input: 19-01-2020 Output: 1579392000示例 1 输入:19-01-2020 Output:1579392000
Example 2 Input: 31122012 Output: 1356912000示例 2 输入:31122012 Output:1356912000
Example 3 Input: 251220202 Output: Unable to convert the provided date示例 3 输入:251220202 Output:无法转换提供的日期
Example 4 Input: 17:04:2020 Output: Unable to convert the provided date示例 4 输入:17:04:2020 Output:无法转换提供的日期
from datetime import datetime
import re
my_input = input("Enter date: ")
my_input = re.sub("/", "-", my_input)
my_input = re.sub(".", "-", my_input)
try:
out = datetime.strptime(my_input, "%d-%m-%Y").timestamp()
except Exception:
try:
out = datetime.strptime(my_input, "%d%m%Y").timestamp()
except Exception:
out = "Unable to convert the provided date"
print(out)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.