How to get current time in python and break up into year, month, day, hour, minute?

后端 未结 11 1036
故里飘歌
故里飘歌 2020-11-28 01:18

I would like to get the current time in Python and assign them into variables like year, month, day, hour, minute

11条回答
  •  醉梦人生
    2020-11-28 02:10

    The datetime module is your friend:

    import datetime
    now = datetime.datetime.now()
    print(now.year, now.month, now.day, now.hour, now.minute, now.second)
    # 2015 5 6 8 53 40
    

    You don't need separate variables, the attributes on the returned datetime object have all you need.

提交回复
热议问题