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. 微信小程序开发之路之组件化

    类似于页面,自定义组件拥有自己的 wxml 模版和 wxss 样式. 官方链接 组件化,反过来理解,写重复的页面,方法,写第二遍就烦了,抽取出来就是组件化,可以理解为公用的方法 对于通用的数据,最先想 ...

  2. 实验吧--隐写术--九连环--WriteUp

    题目: http://ctf5.shiyanbar.com/stega/huan/123456cry.jpg 是一张图: 放到binwalk查看一下 发现存在压缩文件. 使用-e参数将文件分离 打开文 ...

  3. centos7.3挂在移动硬盘(亲测)

    一 下载ntfs-3g wget https://tuxera.com/opensource/ntfs-3g_ntfsprogs-2016.2.22.tgz 二 解压并安装 1 检测是否安装gcc r ...

  4. 基础数据类型汇总补充;集合set ;深浅copy

    首先回顾: 小数据池:int -5~256str 特殊字符,*数字20 ascii : 8位 1字节 表示1个字符unicode 32位 4个字节 表示一个字符utf- 8 1个英文 8位,1个字节 ...

  5. poj 1733 并查集+hashmap

    题意:题目:有一个长度 已知的01串,给出多个条件,[l,r]这个区间中1的个数是奇数还是偶数,问前几个是正确的,没有矛盾 链接:点我 解题思路:hash离散化+并查集 首先我们不考虑离散化:s[x] ...

  6. 1.7(SQL学习笔记)游标

    一.游标简介 SELECT语句得到的是一个结果集,有时我们需要对结果集中的单条数据进行处理. 这时就需要使用游标,游标定义时和一个SELECT语句的结果集关联在一起. 游标执行这个结果集,可以在结果集 ...

  7. Problem G: 切煎饼

    Description 王小二自夸刀工不错,有人放一张大的圆煎饼在砧板上,问他:饼不允许离开砧板,切100刀最多能切多少块? Input 多组测试数据,每组输入1个整数,代表切的刀数 Output 每 ...

  8. PHP -- 8个必备的PHP功能开发

    原文出处:http://www.codeceo.com/8-php-functions.html 做过PHP开发的程序员应该清楚,PHP中有很多内置的功能,掌握了它们,可以帮助你在做PHP开发时更加得 ...

  9. Educational Codeforces Round 10 D. Nested Segments 离线树状数组 离散化

    D. Nested Segments 题目连接: http://www.codeforces.com/contest/652/problem/D Description You are given n ...

  10. 探究react-native 源码的图片缓存

    先看js端图片使用的三种方式,依次排序1.2.3 <Image source={{uri:url}} style={{width:200,height:200}}/> 1. 加载远程图片 ...