Date-Time Module in Python
Naveen
- 0
The data-time is a built-in module supplies classes for manipulating dates and times.
datetime.now
use the now() method to access the current date and time.
![](https://www.nomidl.com/wp-content/uploads/2022/09/image.png)
Date class in datetime module
We can represent the data object using the data class
Syntax to create a data object
datetime.date(year, month, day)
![](https://www.nomidl.com/wp-content/uploads/2022/09/image-1.png)
date.today()
it will return the current local date.
![](https://www.nomidl.com/wp-content/uploads/2022/09/image-2.png)
Time class in datetime module
To work only with time, we can use the time class.
Syntax to create a time object
datetime.time(hour, minute, second, microsecond, tzinfo, *, fold)
The arguments should be in the following range:
- 0 <= hour < 24
- 0 <= minute < 60
- 0 <= second < 60
- 0 <= microsecond < 1000000
- Fold in [0, 1]
- tzinfo is the timezone information.
If we pass an argument outside of given ranges, ValueError is raised.
![](https://www.nomidl.com/wp-content/uploads/2022/09/image-3.png)
datetime.now()
Use datetime.now() to return the local time.
![](https://www.nomidl.com/wp-content/uploads/2022/09/image-4.png)
Popular Posts
Spread the knowledge