/ Published in: Python
Reference of simple operations with dates, such as formatting and arithmetic.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
import datetime if __name__ == '__main__': # printing today's date formatted as dd/mm/yyyy. For more formatting options, search for strftime() in Python documentation print(datetime.date.today().strftime('%d/%m/%Y')) # creating a date object miyazakiBirthDate = datetime.date(1941, 1, 5) print(miyazakiBirthDate.strftime('%d/%m/%Y')) # example of date arithmetic operation. Adding one day to miyazakiBirthDate miyazakiBirthNextDay = miyazakiBirthDate + datetime.timedelta(days=1) print(miyazakiBirthNextDay.strftime('%d/%m/%Y')) # example of date part replacement. Changing the miyazakiBirthDate year to year + 1 miyazakiFirstBirthday = miyazakiBirthDate.replace(year=miyazakiBirthDate.year + 1) print(miyazakiFirstBirthday.strftime('%d/%m/%Y'))