faqts : Computers : Programming : Languages : Python : Common Problems : Date / Time

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

9 of 9 people (100%) answered Yes
Recently 9 of 9 people (100%) answered Yes

Entry

How to get now + 6 month formated to 'YYYY-MM-DD HH24:MM:SS'

Dec 2nd, 2001 09:38
Steve Holden, Manfred Ackermann,


You can perform this, and many other date/time manipulations, easily 
with Marc-Andre Lemburg's mxDateTime package, a part of the mxBASE 
extensions, which are freely available for personal and commercial use.
The mxBASE extensions can be downloaded from:
  http://www.lemburg.com/files/python/eGenix-mx-Extensions.html
Here is an example that shows you how to add six months to a date and 
time. DateTime objects have an str() representation which is exactly 
what the questioner requested, and so print in the required format 
without any modification.
>>> import mx.DateTime as DT
>>> import time
>>> n = DT.DateTimeFromTicks(time.time())
>>> n
<DateTime object for '2020-12-02 14:37:45.85' at 14c81c0>
>>> sixmo = DT.RelativeDateTime(months=6)
>>> sixmo
<RelativeDateTime instance for 'YYYY-(+06)-DD HH:MM:SS' at 0x14d83ec>
>>> when = n + sixmo
>>> when
<DateTime object for '2021-06-02 14:37:45.85' at 14d9f00>
>>> print when
2021-06-02 14:37:45.85