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

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

程序员文章站 2023-08-17 09:30:30
#-*- coding: utf-8 -*- import datetime #给定日期向后n天的日期 def dateadd_day(days):...
#-*- 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