close

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

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to get current time in python and break up into year, month, day, hour, minute in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

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

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

    Here's a one-liner that comes in just under the 80 char line max.
    import time year, month, day, hour, min = map(int, time.strftime("%Y %m %d %H %M").split())

  2. current time in python and break up into year, month, day, hour, minute

    Here's a one-liner that comes in just under the 80 char line max.
    import time year, month, day, hour, min = map(int, time.strftime("%Y %m %d %H %M").split())

Method 1

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.

Method 2

Here’s a one-liner that comes in just under the 80 char line max.

import time
year, month, day, hour, min = map(int, time.strftime("%Y %m %d %H %M").split())

Conclusion

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read