import datetime
datetime.datetime.now().isoformat()
> '2019-10-01T10:37:22.402600'
datetime.datetime.now(datetime.timezone.utc).isoformat()
datetime.datetime.now(datetime.timezone.utc).isoformat('T')
> '2019-10-01T07:36:54.544600+00:00'
datetime.datetime.now(datetime.timezone.utc).astimezone()
> datetime.datetime(2019, 10, 1, 10, 35, 3, 478600, tzinfo=datetime.timezone(datetime.timedelta(0, 10800), 'RTZ 2 (ceia)'))
(Применяется в RSS, Заголовках HTTP Например в Last-Modified)
import datetime
from email.utils import format_datetime
dt = datetime.datetime.now()
res = format_datetime(value, usegmt=False)
> Sat, 25 Sep 2021 21:21:44 +0000
import datetime, time
from email.utils import formatdate
dt = datetime.datetime(2019, 10, 1, 10, 35, 3, 478600)
dt2 = time.mktime(dt.timetuple())
res = formatdate(dt2, usegmt=True)
> Tue, 01 Oct 2019 07:35:03 GMT
dt = datetime.datetime(2019, 10, 1, 10, 35, 3, 478600)
datetime.datetime.strftime(dt, '%a, %d %b %Y %H:%M:%S')
> 'Tue, 01 Oct 2019 10:35:03'
from email.utils import parsedate_to_datetime
datestr = 'Sun, 09 Mar 1997 13:45:00 -0500'
res = parsedate_to_datetime(datestr)
datetime.datetime.strptime('Sun, 09 Mar 1997 13:45:00', '%a, %d %b %Y %H:%M:%S')
now = datetime.datetime.now() # datetime.datetime(2019, 12, 4, 8, 20, 32, 773800)
now.toordinal() # 737397 # Кол-во дней от 0001-01-01T00:00:00
datetime.datetime.fromordinal(737397) # 2019-12-04T00:00:00
now.replace(hour=23,minute=59,second=59,microsecond=0) #datetime.datetime(2019, 12, 4, 23, 59, 59)
from dateutil.relativedelta import relativedelta #relativedelta устанавливает дни, часы в указанное число, скорректировав, если
mydate = datetime.datetime(year=2019, month=2, day=1, hour=0, minute=0, second=0)
mydate + dateutil.relativedelta(day=31, hour=23, minute=59, second=59)
> datetime.datetime(2019, 2, 28, 23, 59, 59)
Теги:
Программирование
Python