欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

Python常用的日期时间处理方法示例

程序员文章站 2022-05-23 12:06:47
...
#-*- coding: utf-8 -*-
import datetime


#给定日期向后N天的日期
def dateadd_day(days):
  d1 = datetime.datetime.now()
  d3 = d1 + datetime.timedelta(days)
  return d3


#昨天
def getYesterday():
  today = datetime.date.today()
  oneday = datetime.timedelta(days=1)
  yesterday = today - oneday
  return yesterday


#今天
def getToday():
  return datetime.date.today()


#获取给定参数的前几天的日期,返回一个list
def getDaysBefore(num):
  today = datetime.date.today()
  oneday = datetime.timedelta(days=1)
  li = []
  for i in range(0, num):
    #今天减一天,一天一天减
    today = today - oneday
    #把日期转换成字符串
    li.append(datetostr(today))
  return li


#将字符串转换成datetime类型
def strtodatetime(datestr, format):
  return datetime.datetime.strptime(datestr, format)


#时间转换成字符串,格式为2015-02-02
def datetostr(date):
  return str(date)[0:10]


#时间转换成字符串,格式为2015-02-02
def datetostr_secod(date):
  return str(date)[0:19]


#两个日期相隔多少天,例:2015-2-04和2015-3-1
def datediff(beginDate, endDate):
  format = "%Y-%m-%d"
  bd = strtodatetime(beginDate, format)
  ed = strtodatetime(endDate, format)
  oneday = datetime.timedelta(days=1)
  count = 0
  while bd != ed:
    ed = ed - oneday
    count += 1
  return count


#两个日期之间相差的秒
def datediff_seconds(beginDate, endDate):
  format = "%Y-%m-%d %H:%M:%S"
  if " " not in beginDate or ':' not in beginDate:
    bformat = "%Y-%m-%d"
  else:
    bformat = format
  if " " not in endDate or ':' not in endDate:
    eformat = "%Y-%m-%d"
  else:
    eformat = format
  starttime = strtodatetime(beginDate, bformat)
  endtime = strtodatetime(endDate, eformat)
  ret = endtime - starttime
  return ret.days * 86400 + ret.seconds


#获取两个时间段的所有时间,返回list
def getDays(beginDate, endDate):
  format = "%Y-%m-%d"
  begin = strtodatetime(beginDate, format)
  oneday = datetime.timedelta(days=1)
  num = datediff(beginDate, endDate) + 1
  li = []
  for i in range(0, num):
    li.append(datetostr(begin))
    begin = begin + oneday
  return li


#获取当前年份 是一个字符串
def getYear(date=datetime.date.today()):
  return str(date)[0:4]


#获取当前月份 是一个字符串
def getMonth(date=datetime.date.today()):
  return str(date)[5:7]


#获取当前天 是一个字符串
def getDay(date=datetime.date.today()):
  return str(date)[8:10]


#获取当前小时 是一个字符串
def getHour(date=datetime.datetime.now()):
  return str(date)[11:13]


#获取当前分钟 是一个字符串
def getMinute(date=datetime.datetime.now()):
  return str(date)[14:16]


#获取当前秒 是一个字符串
def getSecond(date=datetime.datetime.now()):
  return str(date)[17:19]


def getNow():
  return datetime.datetime.now()


print dateadd_day(10)
#2015-02-14 16:41:13.275000

print getYesterday()
#2015-02-03

print getToday()
#2015-02-04

print getDaysBefore(3)
#['2015-02-03', '2015-02-02', '2015-02-01']

print datediff('2015-2-01', '2015-10-05')
#246

print datediff_seconds('2015-02-04', '2015-02-05')
#86400

print datediff_seconds('2015-02-04 22:00:00', '2015-02-05')
#7200

print getDays('2015-2-03', '2015-2-05')
#['2015-02-03', '2015-02-04', '2015-02-05']

print datetostr_secod(getNow())
#2015-02-04 16:46:47

print str(getYear(dateadd_day(-50))) + '-'\
  + getMonth() + '-'\
  + getDay() + ' '\
  + getHour() + ':'\
  + getMinute() + ':'\
  + getSecond()
#2014-02-04 16:59:04

print getNow()
#2015-02-04 16:46:47.454000