I have two date-time string like "1637279999" that converts into "Thursday, 18 November 2021 23:59:59" and "1637193600" that converts into "Thursday, 18 November 2021 00:00:00". I converted using https://www.epochconverter.com/.
Is there any python function that converts directly current date-time (with HH:MM:SS) into the following formats?
String 1: Thursday, 18 November 2021 23:59:59 => 1637279999
String 2: Thursday, 18 November 2021 00:00:00 => 1637193600
CodePudding user response:
Parse the date, add TimeZone info to that, then get the timestamp from it.
from datetime import datetime, timezone
def to_timestamp(date_str):
date_obj = datetime.strptime(date_str, '%A, %d %B %Y %H:%M:%S')
date_obj = date_obj.replace(tzinfo=timezone.utc) # replace your desired TZ here
return date_obj.timestamp()