SimpleDao
Python 时间格式转换
2019-05-06, 访问数: 975

datetime和str

  1. # str转datetime
  2. datetime.datetime.strptime('2017-04-23 00:00:00', '%Y-%m-%d %H:%M:%S')
  3. # datetime转str
  4. datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S');
  5. # datetime转timestamp
  6. time.mktime(dt.timetuple())

utc时间转本地时间

  1. from_zone = tz.tzutc()
  2. to_zone = tz.tzlocal()
  3. utc = datetime.strptime(utcs, '%Y-%m-%dT%H:%M:%SZ')
  4. utc = utc.replace(tzinfo=from_zone)
  5. local_dt = utc.astimezone(to_zone)
  6. dts = local_dt.strftime('%Y-%m-%d %H:%M')

timestamp 转 datetime

  1. datetime.fromtimestamp(ts)
  2. datetime.utcfromtimestamp(ts)