How do I strftime a date object in a different locale?

前端 未结 3 538
半阙折子戏
半阙折子戏 2020-11-29 05:58

I have a date object in python and I need to generate a time stamp in the C locale for a legacy system, using the %a (weekday) and %b (month) codes. However I do not wish to

3条回答
  •  遥遥无期
    2020-11-29 06:27

    take a look to the pytz package

    you can use like this

    import pytz
    UTC = pytz.timezone('UTC') # utc
    fr = pytz.timezone('Europe/Paris') #your local
    from datetime import datetime
    date = datetime.now(fr)
    dateUTC = date.astimezone(UTC)
    

    strftime will render in the timezone specified

    for have month name in the locale use calendar for example :

    import calendar
    print calendar.month_name[dateUTC.month] #will print in the locale
    

    inspect more deeply calendar for having more information

提交回复
热议问题