Search code examples
python-3.xdatetimetimezoneutcpython-datetime

Convert UTC timezone to IST python


The following code line gives me the UTC timing on the production server.

timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')

Please suggest a way to convert above UTC timezone to IST(Indian Standard Time) timing.


Solution

  • from datetime import datetime    
    import pytz    
    tz_NY = pytz.timezone('Asia/Kolkata')   
    datetime_NY = datetime.now(tz_NY)  
    print("India time:", datetime_NY.strftime("%Y-%m-%d %H:%M:%S.%f"))       
    >>India time: 2021-05-03 12:25:21.877976