python获取当前系统时间:

nowTime=time.localtime()

获取当前系统日期:

nowDate=datetime.datetime(nowTime[0],nowTime[1],nowTime[2])

日期格式转换:

baseTime="20121012" #转换成2012-10-12

mouth = baseTime[4:6]
year = baseTime[0:4]
day = baseTime[6:8]
Destime = year +"-"+mouth+"-"+day

获取当前系统时间,并转换成字符串类型:
curTime = time.strftime("%Y-%m-%d %X", time.localtime(time.time()))

计算给定时间delta天后或delta天前的时间:

baseTime="20121012"               #字符串日期

delta=3                                       #计算3天后的日期,如果是delta=-3,计算3天前的日期
d1 = datetime.datetime(string.atoi(baseTime[0:4]),string.atoi(baseTime[4:6]),string.atoi(baseTime[6:8]))
d2 = d1 + datetime.timedelta(days = delta)
deltaDate=d2.strftime("%Y%m%d")

字符串转换成时间 string -> time 和 time  -> string  和 time ->  datetime:

date="2012-04-05"

print  type(date)                  #查看date的类型<type 'str'>

date=time.strptime(date,"%Y-%m-%d")                           #字符串string类型时间转换成time类型时间

print  type(date)                 #查看date的类型<type 'time.struct_time'>

date=time.strftime("%Y-%m-%d",date)                             #time类型时间转换成字符串string类型时间

print type(date) #查看date的类型<type 'str'>

date="2012-04-05"

date=time.strptime(date,"%Y-%m-%d")                            #字符串string类型时间转换成time类型时间

print type(date) #查看date的类型<type 'time.struct_time'>

date=datetime.datetime(date[0],date[1],date[2])               #time类型时间转换成datetime类型时间

print type(date) #查看date的类型<type 'datetime.datetime'>

计算两个日期之间的时间查:

d1 = datetime.datetime(2005, 2, 16)
d2 = datetime.datetime(2004, 12, 31)

# 结果:47
print (d1 - d2).days

#上例演示了计算两个日期相差天数的计算。

starttime = datetime.datetime.now()
....
endtime = datetime.datetime.now()
print (endtime - starttime).seconds

#上例演示了计算运行时间的例子,以秒进行显示。

d1 = datetime.datetime.now()
d3 = d1 + datetime.timedelta(days =10)

print str(d3)
print d3.ctime()

# 上例演示了计算当前时间向后10天的时间。
# 如果是小时 days 换成 hours

python中各类时间的计算的更多相关文章

  1. Python 中的时间处理包datetime和arrow

    Python 中的时间处理包datetime和arrow 在获取贝壳分的时候用到了时间处理函数,想要获取上个月时间包括年.月.日等 # 方法一: today = datetime.date.today ...

  2. python中的时间和时间格式转换

    1.python中的时间:要得到年月日时分秒的时间: import time #time.struct_time(tm_year=2012, tm_mon=9, tm_mday=15, tm_hour ...

  3. python中time()时间的相关问题

    Python中time模块详解(转) 在平常的代码中,我们常常需要与时间打交道.在Python中,与时间处理有关的模块就包括:time,datetime以及calendar.这篇文章,主要讲解time ...

  4. [ Python入门教程 ] Python中日期时间datetime模块使用实例

    Python中datetime模块提供强大易用的日期处理功能,用于记录程序操作或修改时间.时间计算.日志时间显示等功能.datatime模块重新封装了time模块,提供的类包括date.time.da ...

  5. Python中关于日期的计算总结

    1.获取当前时间的两种方法: 代码如下: import datetime,timenow = time.strftime("%Y-%m-%d %H:%M:%S")print now ...

  6. Python中的时间与日期

    本文简要介绍datetime,time模块的简要用法. datetime模块 datetime模块主要有四个主要的对象. date 处理年.月.日 time处理时.分.秒.微秒 datetime处理日 ...

  7. Python中的时间日期模块(time、datetime)

    目录 Datetime 获取当前时间 获取当前日期 获取当前时间的tuple元组 格式化日期和时间 时间移动 获取两个时间的时间差 时间格式转换 Time 获取距元年(1970.1.1)的秒数 当时时 ...

  8. Python中对时间日期的处理方法简单汇总

    这篇文章主要介绍了Python实用日期时间处理方法汇总,本文讲解了获取当前datetime.获取当天date.获取明天/前N天.获取当天开始和结束时间(00:00:00 23:59:59).获取两个d ...

  9. python中的时间转换

    Python中的时间相关库有: datetime 和time. 主要形式有: datetime timestamp 相互转换: timestamp->datetime: dt = datetim ...

随机推荐

  1. COMP COMP-3

    Comp (Computational) Comp (with no suffix) leaves the choice of the data type to the compiler writer ...

  2. Scrapy实战篇(五)爬取京东商城文胸信息

    创建scrapy项目 scrapy startproject jingdong 填充 item.py文件 在这里定义想要存储的字段信息 import scrapy class JingdongItem ...

  3. codevs 2181 田忌赛马

    2181 田忌赛马 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 钻石 Diamond   题目描述 Description 中国古代的历史故事“田忌赛马”是为大家所熟知的.话说齐王 ...

  4. 【BFS】【map】hdu5925 Coconuts

    题意:一张n*m的网格图(n和m可以达到10^9),其中K个点是障碍物(不超过200个),问你没有被障碍物占据的点形成了几个连通块?并且输出各个连通块的大小. 容易证明,大小超过40000的连通块最多 ...

  5. springmvc+hibernate4事务管理配置

    1.事务的特性 事务的四种特性: 原子性:体现一个事务的操作的不可分割,要么权执行,要么全不执行. 一致性:事务的执行结果必须从一种一致性状态变到另一种一致性状态.最典型的就是转账,两个账户A.B总金 ...

  6. 两个矩阵相加 Exercise08_05

    import java.util.Scanner; /** * @author 冰樱梦 * 时间:2018年12月 * 题目:两个矩阵相加 * */ public class Exercise08_0 ...

  7. linq to datatable 和lambda查询datatable

    用Linq查询DataTable static DataTable table = new DataTable(); static DataColumn dc = new DataColumn(); ...

  8. Sencha Touch开发环境搭建及ext插件Spket安装

    第一步:JAVA SDK(JDK)的安装 以去问百度下载JDK,也可以到官方下载JDK. 下载地址: http://www.oracle.com/technetwork/java/javase/dow ...

  9. Struts2 学习笔记 09 访问Web元素

    我们想要访问Map类型request,session,application.真实类型HttpServletRequest,HttpSession,ServletContext的引用,并对它们进行操作 ...

  10. ubuntu下从源码编译安装cherokee

    apt-get install automake autoconf libtool rrdtool libpam0g-dev libssl-dev libgeoip-dev libldap2-dev ...