python时间处理
1.获取当前时间的两种方法:
import datetime,time
now = time.strftime("%Y-%m-%d %H:%M:%S")
print now
now = datetime.datetime.now()
print now
2.获取上个月最后一天的日期(本月的第一天减去1天)
last = datetime.date(datetime.date.today().year,datetime.date.today().month,1)-datetime.timedelta(1)
print last
3.获取时间差(时间差单位为秒,常用于计算程序运行的时间)
starttime = datetime.datetime.now()
#long running
endtime = datetime.datetime.now()
print (endtime - starttime).seconds
4.计算当前时间向后10个小时的时间
d1 = datetime.datetime.now()
d3 = d1 + datetime.timedelta(hours=10)
d3.ctime()
其本上常用的类有:datetime和timedelta两个。它们之间可以相互加减。每个类都有一些方法和属性可以查看具体的值,如 datetime可以查看:天数(day),小时数(hour),星期几(weekday())等;timedelta可以查看:天数(days),秒数 (seconds)等。
5.python中时间日期格式化符号:
%y 两位数的年份表示(00-99)
%Y 四位数的年份表示(000-9999)
%m 月份(01-12)
%d 月内中的一天(0-31)
%H 24小时制小时数(0-23)
%I 12小时制小时数(01-12)
%M 分钟数(00=59)
%S 秒(00-59)
%a 本地简化星期名称
%A 本地完整星期名称
%b 本地简化的月份名称
%B 本地完整的月份名称
%c 本地相应的日期表示和时间表示
%j 年内的一天(001-366)
%p 本地A.M.或P.M.的等价符
%U 一年中的星期数(00-53)星期天为星期的开始
%w 星期(0-6),星期天为星期的开始
%W 一年中的星期数(00-53)星期一为星期的开始
%x 本地相应的日期表示
%X 本地相应的时间表示
%Z 当前时区的名称
%% %号本身
附上示例代码:
代码Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->#-*-coding:utf-8-*-
import datetime, calendar
def getYesterday():
today=datetime.date.today()
oneday=datetime.timedelta(days=1)
yesterday=today-oneday
return yesterday
def getToday():
return datetime.date.today()
#获取给定参数的前几天的日期,返回一个list
def getDaysByNum(num):
today=datetime.date.today()
oneday=datetime.timedelta(days=1)
li=[]
for i in range(0,num):
#今天减一天,一天一天减
today=today-oneday
#把日期转换成字符串
#result=datetostr(today)
li.append(datetostr(today))
return li
#将字符串转换成datetime类型
def strtodatetime(datestr,format):
return datetime.datetime.strptime(datestr,format)
#时间转换成字符串,格式为2008-08-02
def datetostr(date):
return str(date)[0:10]
#两个日期相隔多少天,例:2008-10-03和2008-10-01是相隔两天
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
#获取两个时间段的所有时间,返回list
def getDays(beginDate,endDate):
format="%Y-%m-%d";
bd=strtodatetime(beginDate,format)
ed=strtodatetime(endDate,format)
oneday=datetime.timedelta(days=1)
num=datediff(beginDate,endDate)+1
li=[]
for i in range(0,num):
li.append(datetostr(ed))
ed=ed-oneday
return li
#获取当前年份 是一个字符串
def getYear():
return str(datetime.date.today())[0:4]
#获取当前月份 是一个字符串
def getMonth():
return str(datetime.date.today())[5:7]
#获取当前天 是一个字符串
def getDay():
return str(datetime.date.today())[8:10]
def getNow():
return datetime.datetime.now()
print getToday()
print getYesterday()
print getDaysByNum(3)
print getDays('2008-10-01','2008-10-05')
print '2008-10-04 00:00:00'[0:10]
print str(getYear())+getMonth()+getDay()
print getNow()
python时间处理的更多相关文章
- 浅谈Python时间模块
浅谈Python时间模块 今天简单总结了一下Python处理时间和日期方面的模块,主要就是datetime.time.calendar三个模块的使用.希望这篇文章对于学习Python的朋友们有所帮助 ...
- python时间处理之datetime
python时间处理之datetime 标签: pythondateimportstringc 2012-09-12 23:21 20910人阅读 评论(0) 收藏 举报 分类: Python系列( ...
- python 时间字符串与日期转化
python 时间字符串与日期转化 datetime.datetime.strptime(string, format) 根据指定的格式解析字符串为一个datetime类型.相当于datetime.d ...
- python时间日期字符串各种
python时间日期字符串各种 第一种 字符串转换成各种日期 time 库 # -*- coding: utf-8 -*- import time, datetime # 字符类型的时间 tss1 = ...
- Python 时间日历类型
# 时间日历 # time模块 # 提供了处理时间和表示之间转换的功能 # 获取当前时间戳 # 概念 # 从0时区的1970年1月1日0时0分0秒, 到所给定日期时间的秒数 # 浮点数 # 获取方式 ...
- Python时间与日期操作(datetime、time、calendar)
相关模块 模块 说明 time time是一个仅包含与日期和时间相关的函数和常量的模块,在本模块中定义了C/C++编写的几个类.例如,struct_time类 datetime datetime是一个 ...
- [python] 时间序列分析之ARIMA
1 时间序列与时间序列分析 在生产和科学研究中,对某一个或者一组变量 进行观察测量,将在一系列时刻 所得到的离散数字组成的序列集合,称之为时间序列. 时间序列分析是根据系统观察得到的时间序列数据, ...
- python—时间与时间戳之间的转换
python-时间与时间戳之间的转换 对于时间数据,如2016-05-05 20:28:54,有时需要与时间戳进行相互的运算,此时就需要对两种形式进行转换,在Python中,转换时需要用到time模块 ...
- python时间格式化
import timeprint time.time()输出的结果是:1279578704.6725271 但是这样是一连串的数字不是我们想要的结果,我们可以利用time模块的格式化时间的方法来处理: ...
- Python时间处理之time模块
1.time模块简介 time模块提供各种操作时间的函数 说明:一般有两种表示时间的方式: 第一种是时间戳的方式(相对于1970.1.1 00:00:00以秒计算的偏移量),时间戳是惟一 ...
随机推荐
- XRDP与VNC的关系
如果仅仅安装XRDP协议.是不能在windows上使用远程桌面连接到Ubuntu. 还须要安装VNCServer才行. 所以,XRDP启动之后.系统会自己主动启动一个VNC会话进程监听服务. 当我们通 ...
- [Redux] Wrapping dispatch() to Log Actions
We will learn how centralized updates in Redux let us log every state change to the console along wi ...
- mybatis01
mybatis是一个java持久层框架,java中操作关系型 数据库用的是jdbc,mybatis是对jdbc的一个封装. jdk1..0_72 eclipse:eclipse-3.7-indigo ...
- GDB 运行PYTHON 脚本+python 转换GDB调用栈到流程图
http://tromey.com/blog/?cat=17 http://blog.csdn.net/cnsword/article/details/16337031 http://blog.csd ...
- 以色列学生---debugger 构建
http://eli.thegreenplace.net/2011/01/23/how-debuggers-work-part-1 http://eli.thegreenplace.net/
- 非常全面的java基础笔试题
下面是java基础笔试题,当时我去笔试,做了1个小时(80道选择题,后面的简答题就没时间做了),结果很吓人,太挫了,最后被面试官忽悠去培训去了,呵呵.我偷偷把面试题弄了下来,用来学习吧,也希望能对你们 ...
- Android开发之使用意图调用内置应用程序
意图可以调用活动,也常被用来调用内置应用程序,如加载web页面,拨号页面,内置地图应用等等.下面就用例子来说明该用法. 效果图如下: 实现代码如下: 上图中的启动MyBrowser是用意图来调用MyW ...
- Linux下安装并破解StarUML
下载 官网地址: http://starumlreleases-7a0.kxcdn.com/v2.7.0/StarUML-v2.7.0-64-bit.deb CSDN: http://download ...
- python 入门1
python的历史 Python是一种解释型.面向对象.动态数据类型的高级程序设计语言. Python由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991年. 像Per ...
- Table显示滚动条
Table显示滚动条,要先把table放到一个div中,div的长度和宽度要固定,控制overflow属性为scroll <div style="width:700px; height ...