##python时间操作一般使用time、datetime两个模块

对于time模块,时间的表示模式有3种
1、时间戳:time.time()
2、字符串: time.strftime('%Y%m%d')
3、struct_time格式: time.localtime()

如下所示:

 #时间操作
>>> import time
>>> time.time()
1450336566.81052
>>> time.localtime()
time.struct_time(tm_year=2015, tm_mon=12, tm_mday=17, tm_hour=15, tm_min=16, tm_sec=14, tm_wday=3, tm_yday=351, tm_isdst=0)
>>> time.strftime('%Y%m%d %H%M%S')
'20151217 151632'
>>> time.strptime('20151212 121212','%Y%m%d %H%M%S')
time.struct_time(tm_year=2015, tm_mon=12, tm_mday=12, tm_hour=12, tm_min=12, tm_sec=12, tm_wday=5, tm_yday=346, tm_isdst=-1)
>>> time.mktime(time.localtime())
1450336685.0
>>>
>>> yesterday = time.strftime('%Y-%m-%d 00:00:00',time.localtime(time.time()-3600*24))
>>> print yesterday
2015-12-16 00:00:00
>>> tomorrow = time.strftime('%Y-%m-%d 00:00:00',time.localtime(time.time()+3600*24))
>>> print tomorrow
2015-12-18 00:00:00

datetime对于时间计算很有用
datetime模块下有几个比较有用的方法 datetime,date,time,timedelta
语法: datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])
date(year, month, day) --> date object
time([hour[, minute[, second[, microsecond[, tzinfo]]]]]) --> a time object
timedelta(days=1,hours=2,minutes=3,seconds=4)
calendar.monthrange(year, month):判断由year和month组成月份,返回该月第一天为周几和该月总共有多少天

 ##取日期列表
from datetime import datetime,date,timedelta
def get_range_time(begin,end,step):
while begin < end:
yield begin
begin = begin + step for i in get_range_time(datetime(2015,11,2),datetime(2016,3,2),timedelta(days=1)):
print i from datetime import datetime,date,timedelta
import calendar #取动态月(自然月需要置day=1),如下
def get_month_range(startdate = None):
if startdate is None:
startdate = date.today().replace(day=1)
_,days_in_month = calendar.monthrange(startdate.year,startdate.month)
enddate = startdate + timedelta(days=days_in_month)
return startdate,enddate begin,end = get_month_range()
add_step = timedelta(days=1)
while begin < end:
print begin
begin = begin + add_step

请记住以下时间转换关系图

python 之日期时间处理的更多相关文章

  1. Python学习---日期时间

    在Python里面日期时间的功能主要由几个模块提供:time,calendar,datetime,date等 time主要用到的功能函数: #!/usr/bin/python3 # coding:ut ...

  2. Python实用日期时间处理方法汇总

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

  3. 程序员常用6 个 Python 的日期时间库

    内建的 datetime 模块 在跳转到其他库之前,让我们回顾一下如何使用 datetime 模块将日期字符串转换为 Python datetime 对象. 假设我们从 API 接受到一个日期字符串, ...

  4. Python基础 | 日期时间操作

    目录 获取时间 时间映射 格式转换 字符串转日期 日期转字符串 unixtime 时间计算 时间偏移 时间差 "日期时间数据"作为三大基础数据类型之一,在数据分析中会经常遇到. 本 ...

  5. Python数值日期时间笔记

    数值: 格式化 小数位的处理 随机数: random.choice() 序列中随机选择一个值 random.sample() 获取指定数目的序列 random.shuffle() 打乱顺序 rando ...

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

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

  7. Python中日期时间案例演示

    案例:准备10个人姓名,然后为这10个人随机生成生日[都是90后] 1.统计出那些人是夏季[6月-8月]出生的. 2.最大的比最小的大多少天 3.谁的生日最早,谁的生日最晚 备注:春季[3-5]夏季[ ...

  8. python输出日期时间

    import datetime base = datetime.datetime.today() , ): print(base + datetime.timedelta(days=x))

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

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

随机推荐

  1. globalCompositeOperation 学习

    globalCompositeOperation globalCompositeOperation即Canvas中的合成操作. 1.source-over 这是默认值,他表示绘制的图形将画在现有画布之 ...

  2. linux系统find命令使用

    find命令简介 1.find的作用 find是个使用频率比较高的命令.常常用它在系统特定目录下,查找具有某种特征的文件. 2.    find命令的格式 find  [-path……]-option ...

  3. Java引用数据类型

    值传递:方法调用时,实际参数把它的值传递给对应的形式参数,方法执行中形式参数值的改变不影响实际参 数的值.引用传递:也称为传地址.方法调用时,实际参数的引用(地址,而不是参数的值)被传递给方法中相对应 ...

  4. VI 配置文件(略全)

    配置 ~/.vimrc文件. root则放到/etc/vimrc 具体详见代码 "====================================================== ...

  5. atoi函数和atof函数

    1.函数名:atoi 功能:是把字符串转换成整型数的一个函数,应用在计算机程序和办公软件中 名字来源:alphanumeric to integer 用法:int atoi(const char *n ...

  6. 复习-C语言内嵌汇编-初级(1)

    打印hello world并改变变量i的值 # include <stdio.h> int main() { ; __asm__( "mov %0, #4\n" :&q ...

  7. Linux----mktemp命令的用法

    命令说明: mktemp命令用于临时文件和临时目录.它的主要特点就是可以做到每次执行mktemp时产生文件和目录都不重名: 这个特性就保证了多个session执行同一脚本都是安全的. 命令用法: 格式 ...

  8. Oracle单个数据文件超过32G后扩容

    Oracle单个数据文件超过32G后扩容   表空间数据文件容量与DB_BLOCK_SIZE的设置有关,而这个参数在创建数据库实例的时候就已经指定.DB_BLOCK_SIZE参数可以设置为4K.8K. ...

  9. 几家SIEM

    HP Arcsight Imperva is a HP Business Partner. HP is the world's largest IT company, providing infras ...

  10. 去英国Savile Row 做件私人定制手工西装_GQ男士网

    去英国Savile Row 做件私人定制手工西装_GQ男士网 去英国Savile Row 做件私人定制手工西装