Setting timezone in Python

后端 未结 5 1814
我在风中等你
我在风中等你 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:12

    It's not an answer, but...

    To get datetime components individually, better use datetime.timetuple:

    time = datetime.now()
    time.timetuple()
    #-> time.struct_time(
    #    tm_year=2014, tm_mon=9, tm_mday=7, 
    #    tm_hour=2, tm_min=38, tm_sec=5, 
    #    tm_wday=6, tm_yday=250, tm_isdst=-1
    #)
    

    It's now easy to get the parts:

    ts = time.timetuple()
    ts.tm_year
    ts.tm_mon
    ts.tm_mday
    ts.tm_hour
    ts.tm_min
    ts.tm_sec
    

提交回复
热议问题