Setting timezone in Python

后端 未结 5 1836
我在风中等你
我在风中等你 2020-11-27 13:47

Is it possible with Python to set the timezone just like this in PHP:

date_default_timezone_set(\"Europe/London\");
$Year = date(\'y\');
$Month = date(\'m\')         


        
5条回答
  •  南笙
    南笙 (楼主)
    2020-11-27 14:09

    >>> import os, time
    >>> time.strftime('%X %x %Z')
    '12:45:20 08/19/09 CDT'
    >>> os.environ['TZ'] = 'Europe/London'
    >>> time.tzset()
    >>> time.strftime('%X %x %Z')
    '18:45:39 08/19/09 BST'
    

    To get the specific values you've listed:

    >>> year = time.strftime('%Y')
    >>> month = time.strftime('%m')
    >>> day = time.strftime('%d')
    >>> hour = time.strftime('%H')
    >>> minute = time.strftime('%M')
    

    See here for a complete list of directives. Keep in mind that the strftime() function will always return a string, not an integer or other type.

提交回复
热议问题