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

作者:junjie 时间:2023-07-30 04:31:05 


#-*- 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
标签:Python,日期时间
0
投稿

猜你喜欢

  • 什么样的图标更具有可用性

    2007-10-16 17:47:00
  • python错误提示:Errno 2] No such file or directory的解决方法

    2022-03-30 10:46:26
  • Python+pyplot绘制带文本标注的柱状图方法

    2023-06-26 16:09:11
  • Python制作动态字符图的实例

    2023-11-02 07:17:18
  • Pytorch平均池化nn.AvgPool2d()使用方法实例

    2023-09-30 02:49:35
  • 手把手教你使用Python解决简单的zip文件解压密码

    2021-01-20 10:08:41
  • python爬虫 爬取超清壁纸代码实例

    2021-03-19 02:51:54
  • python利用元类和描述器实现ORM模型的详细步骤

    2023-11-13 14:54:12
  • NumPy统计函数的实现方法

    2021-10-15 22:02:27
  • Pygame实战之实现扎气球游戏

    2023-06-17 10:17:43
  • Javascript调用XML制作连动下拉框

    2007-09-17 12:55:00
  • 107条javascript(js)常用小技巧

    2007-10-30 12:40:00
  • Zabbix实现监控多个mysql过程解析

    2024-01-25 20:24:53
  • Django权限系统auth模块用法解读

    2021-03-22 13:27:00
  • Python类和实例的属性机制原理详解

    2022-12-12 19:47:27
  • Python实现国外赌场热门游戏Craps(双骰子)

    2023-12-13 23:22:21
  • 使用python批量修改XML文件中图像的depth值

    2023-03-05 12:59:06
  • go sync Once实现原理示例解析

    2023-07-01 12:21:13
  • 如何用变量实现群聊和悄悄话?

    2010-05-19 21:33:00
  • python爬取网页版QQ空间,生成各类图表

    2023-01-18 06:30:58
  • asp之家 网络编程 m.aspxhome.com