DateTime

Date and Time

- In python, we can import a module named datetime to work with date and time as datetime objects.

- Create a variable now, we use the datetime module and class to call the now() method.

- In the below code, we got the current date and time:


Output:

Format- YYYY-MM-DD HH:MM:SS
                  


Creating Date object:

- To create a date object, Create a variable x, we use the datetime module to call the date() class.

- The date() class required 3 parameters (year, month, day) to create a date object.



Creating Datetime object:

To create a datetime object, Create a variable x, we use the datetime module to call the datetime() class.

- The datetime() class required 6 parameters (year, month, day, hour, minute, second) to create a datetime object.


Display some Properties of the given Date:

Year property return the Year of the given Date.

Month property returns the Month of the given Date.

Day property returns on the Day of the given Date.

Weekday method return the week of the given Date as a number (Sunday is 0, Monday is 1 and other weekdays see in the below code).



Display some Properties of the given Datetime:

- Hour property returns the Hour of the given Datetime.

- Minute property returns the Minute of the given Datetime.

- Second property returns the Second of the given Datetime.

Comments