How to get the current time in Python

前端 未结 30 1834
滥情空心
滥情空心 2020-11-22 06:47

What is the module/method used to get the current time?

30条回答
  •  故里飘歌
    2020-11-22 07:36

    Because no one has mentioned it yet, and this is something I ran into recently... a pytz timezone's fromutc() method combined with datetime's utcnow() is the best way I've found to get a useful current time (and date) in any timezone.

    from datetime import datetime
    
    import pytz
    
    
    JST = pytz.timezone("Asia/Tokyo")
    
    
    local_time = JST.fromutc(datetime.utcnow())
    

    If all you want is the time, you can then get that with local_time.time().

提交回复
热议问题