How to get the current time in Python

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

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

30条回答
  •  盖世英雄少女心
    2020-11-22 07:31

    This question is for Python but since Django is one of the most widely used frameworks for Python, its important to note that if you are using Django you can always use timezone.now() instead of datetime.datetime.now(). The former is timezone 'aware' while the latter is not.

    See this SO answer and the Django doc for details and rationale behind timezone.now().

    from django.utils import timezone
    
    now = timezone.now()
    

提交回复
热议问题