Python DateTime

Submitted by code_admin on Sat, 01/12/2019 - 12:07

import pytz
import datetime
from dateutil.parser import parse

Get a UTC datetime object from isoformat string

  1.     dt = parse(result2JSON['jwtData']['TokenExpiry'])
  2.     dateTimeObjFromJSON = dt.astimezone(pytz.utc)
  3.     print(dateTimeObjFromJSON)

Output iso

  1. self.lastRunDate.isoformat()

Time Addition

  1.     createPersonTime = datetime.now(pytz.timezone("UTC"))
  2.     updatePersonTime = createPersonTime + datetime.timedelta(seconds=int(12))

Flask field

  1. 'nextScheduledRun': fields.DateTime(dt_format=u'iso8601', description='Next scheudled run'),

Base App Set Test datetime

  1. testTime = datetime.datetime.now(pytz.timezone("UTC"))
  2. appObj.setTestingDateTime(testTime)

or constant

  1. testTime = pytz.timezone('UTC').localize(datetime.datetime(2020,1,14,23,3,1,0))
  2. appObj.setTestingDateTime(testTime)

Now calls to

  1. self.appObj.getCurDateTime()

are set

Seconds between times

  1. import datetime as dt
  2.  
  3. a = dt.datetime(2013,12,30,23,59,59)
  4. b = dt.datetime(2013,12,31,23,59,59)
  5.  
  6. (b-a).total_seconds()

Tags

RJM Article Type
Quick Reference