【问题标题】:Convert datetime string with timezone value to Indian Standard Time (IST) [duplicate]将具有时区值的日期时间字符串转换为印度标准时间 (IST) [重复]
【发布时间】:2019-05-23 15:30:20
【问题描述】:

我有一个字符串,它表示带有时区的日期时间,我试图通过解析字符串将其转换为印度标准时间 (IST)。

日期字符串是,

"2018-12-24T02:35:16-08:00"

我怎么知道 IST 的实际时间是多少?

【问题讨论】:

    标签: python python-datetime


    【解决方案1】:
    from datetime import datetime
    from dateutil import tz
    utc_tz= tz.gettz('UTC')
    india_tz= tz.gettz('Asia/Kolkata')
    date_string="2018-12-24T02:35:16-08:00"
    #utc = datetime.strptime(date_string[:date_string.rindex('-')].replace('T',' '), '%Y-%m-%d %H:%M:%S')
    # or
    utc = datetime.strptime(date_string[:date_string.rindex('-')], '%Y-%m-%dT%H:%M:%S')
    utc = utc.replace(tzinfo=utc_tz)
    india_time_with_offset = utc.astimezone(india_tz)
    india_time_without_offset = india_time_with_offset.replace(tzinfo=None)
    print(india_time_with_offset)
    print(india_time_without_offset)
    

    输出

    2018-12-24 08:05:16+05:30
    2018-12-24 08:05:16
    

    【讨论】:

      猜你喜欢
      • 2013-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多