w3resource

Python: Get the current date time information

Python Datetime: Exercise-40 with Solution

Write a Python program to get the current date and time information.

Sample Solution:-

Python Code:

# Import the time module
import time
# Import the datetime module
import datetime

# Print an empty line
print()
# Print the time in seconds since the epoch
print("Time in seconds since the epoch: %s" %time.time())
# Print the current date and time
print("Current date and time: " , datetime.datetime.now())
# Print an alternate date and time format
print("Alternate date and time: " ,datetime.datetime.now().strftime("%y-%m-%d-%H-%M"))
# Print the current year
print("Current year: ", datetime.date.today().strftime("%Y"))
# Print the month of the year
print("Month of year: ", datetime.date.today().strftime("%B"))
# Print the week number of the year
print("Week number of the year: ", datetime.date.today().strftime("%W"))
# Print the weekday of the week (0 for Sunday, 6 for Saturday)
print("Weekday of the week: ", datetime.date.today().strftime("%w"))
# Print the day of the year
print("Day of year: ", datetime.date.today().strftime("%j"))
# Print the day of the month
print("Day of the month : ", datetime.date.today().strftime("%d"))
# Print the day of the week
print("Day of week: ", datetime.date.today().strftime("%A"))
# Print an empty line
print()

Output:

Time in seconds since the epoch: 1494232844.031525                                                            
Current date and time:  2017-05-08 14:10:44.031541                                                            
Alternate date and time:  17-05-08-14-10                                                                      
Current year:  2017                                                                                           
Month of year:  May                                                                                           
Week number of the year:  19                                                                                  
Weekday of the week:  1                                                                                       
Day of year:  128                                                                                             
Day of the month :  08                                                                                        
Day of week:  Monday 

Explanation:

In the exercise above,

  • The code imports the "time" and "datetime" modules.
  • It prints the time in seconds since the epoch using time.time() function.
  • It prints the current date and time using datetime.datetime.now() function.
  • It prints an alternate date and time format using datetime.datetime.now().strftime("%y-%m-%d-%H-%M").
  • It prints the current year using datetime.date.today().strftime("%Y").
  • It prints the month of the year using datetime.date.today().strftime("%B").
  • It prints the week number of the year using datetime.date.today().strftime("%W").
  • It prints the weekday of the week (0 for Sunday, 6 for Saturday) using datetime.date.today().strftime("%w").
  • It prints the day of the year using datetime.date.today().strftime("%j").
  • It prints the day of the month using datetime.date.today().strftime("%d").
  • It prints the day of the week using datetime.date.today().strftime("%A").

Flowchart:

Flowchart: Get the current date time information.

Python Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Python program to calculate an age in year.
Next: Write a python program to generate a date and time as a string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python-exercises/date-time-exercise/python-date-time-exercise-40.php