Hello, Python developer!
Hope you're fine today! Do you know how can we express the current time in Python program?
Python has 'datetime' module for dealing with the date and time.
Let's test the it with following codes.
import datetime # Load 'datetime' module
current_date_time = datetime.datetime.now( ) # Get current date & time
print(current_date_time) #Print current date & time
Run result
Here are 'timedelta' function examples.
import datetime
current_date_time = datetime.datetime.now( )
print( current_date_time + datetime.timedelta(seconds = 75)) # Add 75 seconds
print( current_date_time - datetime.timedelta(minutes = 25)) # Sub 25 minutes
print( current_date_time + datetime.timedelta(hours =7)) # Add 7 hours
print( current_date_time - datetime.timedelta(days=100)) # Sub 100 days
print( current_date_time + datetime.timedelta(weeks = 8)) # Add 8 weeks
Run result
Please refer following 'datetime' reference documentation from Python official site.
No comments:
Post a Comment