Henry Fok
Henry Fok

Reputation: 65

Python sysdate modification

I am trying to get current system date in python with datetime of which I have to go

   datetime.datetime.now().strftime("%d-%b-%Y")

now I would like to try to access the same thing but 10 years from now... so for example

   datetime.datetime.now().strftime("%d-%b- (%Y + 10)")

So if the current year is 2014, I would like it to give me 2024

Thanks in advance!

Upvotes: 0

Views: 1037

Answers (2)

okayama-taro
okayama-taro

Reputation: 65

2024, python3.12 datetime module

import datetime
datetime.datetime.now()

from datetime import datetime
datetime.now()

same datetime has difernence, module & type.

import datetime
print(type(datetime))       #<class 'module'>
import datetime as dt
print(type(dt))             #<class 'module'>
print(type(datetime))       #<class 'module'>

from datetime import datetime, date, timedelta, timezone
print(type(datetime))       #<class 'type'>
print(type(date))           #<class 'type'>
print(type(timedelta))      #<class 'type'>
print(type(timezone))       #<class 'type'>

print(dt.datetime.now())    #2024-02-22 00:00:23.575098
print(datetime.now())       #2024-02-22 00:00:23.579122

Upvotes: 0

anon582847382
anon582847382

Reputation: 20371

You can increment dates using timedelta objects.

>>> years = 10
>>> now = (datetime.datetime.now() + datetime.timedelta(days=365.2425 * years))
>>> now.strftime('%d-%b-%Y')
'14-Mar-2024'

You could also do (handling the ValueError for February 29th):

>>> now = datetime.datetime.now() 
>>> now.replace(year=now.year+years)

Upvotes: 1

Related Questions