将本地时间戳转换为python中的unix epoch时间戳

问题描述 投票:0回答:2

我有以下格式的日期字符串,其格式为(Asian / Calcutta)时区,格式为(GMT + 05:30),我想将其转换为gmt时区,并找到该字符串的纪元时间戳。

输入 :(本地时间)2019-11-09 10:10:10

Output :(以GMT时间为单位)1573274410

python datetime timestamp unix-timestamp epoch
2个回答
0
投票

Python 3.3 +

import pytz 
import datetime
dt = datetime.datetime.strptime('2019-09-30 09:00', '%Y-%m-%d %H:%M')
ts = pytz.utc.localize(dt).timestamp()

-2
投票

我找到了解决方案

输入:(以当地时间为准)2019-11-09 10:10:10

输出:(以GMT时间为单位)1573274410

import calender
import time
from dateTime import dateTime

LocalTime = "2019-11-09 10:10:10"

GmtTime = datetime.fromtimestamp(LocalTime, timezone('Asia/Calcutta').strftime("%Y-%m-%d %H:%M:%S")

GmtTimeStamp = calendar.timegm(time.strptime(str(GmtTime), "%Y-%m-%d %H:%M:%S"))

它解决了我的问题。

© www.soinside.com 2019 - 2024. All rights reserved.