Python处理时间 time  &&  datetime 模块

个人整理,获取时间方式:

import datetime
import time #获取当前时间:Thu Nov 03 16:40:00 2016
print time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()) #获取当前时间:2016-11-03 16:40:00
print datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') #获取年,月,日:2016-11-03
print datetime.date.today() #获取当前时间:2016-11-03 16:43:14.550000
print datetime.datetime.now() #不加参数是00:00,参数days=1表示一天:1 day, 0:00:00
print datetime.timedelta(days=1) #获取昨天日期:2016-11-02
nowtime=datetime.date.today()
oldtime=datetime.timedelta(days=1)
print(nowtime-oldtime) #获取昨天的精确日期
oldtime=datetime.timedelta(days=1)
print (datetime.datetime.now() - oldtime)

python时间处理之time模块:

import time
【返回时间戳】
print(time.time()) 【返回当前时间】
print(time.ctime()) 【返回一天前的时间】
print(time.ctime(time.time()-86400)) 【函数返回time.struct_time类型的对象】
time_obj = time.gmtime()
print(time_obj)
结果:time.struct_time(tm_year=2016, tm_mon=7, tm_mday=27, tm_hour=8, tm_min=52, tm_sec=26, tm_wday=2, tm_yday=209, tm_isdst=0)
格式化输出:
print(time_obj.tm_year,time_obj.tm_mon,time_obj.tm_mday) print("{year}-{month}".format(year=time_obj.tm_year,month=time_obj.tm_mon)) 【以time.struct_time类型,打印本地时间】
print(time.localtime()) 【转换成时间戳】
time_obj = time.gmtime()
print(time.mktime(time_obj)) 【延时2秒】
time.sleep(2) 【打印UTC,世界标准时间,北京时区是东八区,领先UTC八个小时】
print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime())) 【本地时间】
print(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())) 【把time.struct_time类型时间,转换成时间戳】
tm = time.strptime("2016-05-6 15:06:33","%Y-%m-%d %H:%M:%S")
print(tm)
print(time.mktime(tm))

python时间处理之datetime模块:

import datetime

【打印当前,年,月,日】
print(datetime.date.today()) 【打印当前时间,精确到微秒】
current_time = datetime.datetime.now()
print(current_time) 【转成time.struct_time格式时间】
current_time = datetime.datetime.now()
print(current_time.timetuple()) 【加十天】
print(datetime.datetime.now() +datetime.timedelta(days=10))
【减十天】
print(datetime.datetime.now() +datetime.timedelta(days=-10))
【减十个小时】
print(datetime.datetime.now() +datetime.timedelta(hours=-10))
【加120s】
print(datetime.datetime.now() +datetime.timedelta(seconds=120)) 【替换成指定的时间】
cr_time = datetime.datetime.now()
print(cr_time.replace(2014,9,12))
结果:2014-09-12 17:28:17.522893 【格式化输出】
print(datetime.datetime.strptime("21/11/06 16:30","%d/%m/%y %H:%M")) 【替换成指定时间后,类型是<class 'datetime.datetime'>】
current_time = datetime.datetime.now()
time_obj = current_time.replace(2015,5)
print(time_obj,type(time_obj))
结果:2015-05-27 17:34:13.350245 <class 'datetime.datetime'> 【对比时间大小,取指定时间范围使用】
current_time = datetime.datetime.now()
time_obj = current_time.replace(2015,5)
print(current_time>time_obj)

获取昨天的日期

import datetime
def getYesterday():
today=datetime.date.today()
oneday=datetime.timedelta(days=1)
yesterday=today-oneday
return yesterday # 输出
print(getYesterday())

Python处理时间 time && datetime 模块的更多相关文章

  1. python time 和 datetime模块

    time模块 时间相关的操作,时间有三种表示方式: 时间戳               1970年1月1日之后的秒,即:time.time() 格式化的字符串    2014-11-11 11:11, ...

  2. python time 和 datetime 模块

    时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量.我们运行“type(time.time())”,返回的是float类型. 格式化的时间字 ...

  3. python time、datetime模块

    时间的三种格式:1)时间戳 2)格式化的时间字符串 3)元组(struct_time):time.struct_time(tm_year=1970, tm_mon=5, tm_mday=23, tm_ ...

  4. python time 和 datetime 模块的简介

    时间处理 time 和 datetime import timeimport datetimeprint time.time() #时间戳显示为1508228106.49print time.strf ...

  5. python内建datetime模块

    datetime 获取当前日期和时间 from datetime import datetime now = datetime.now() print(now) datetime转换为timestam ...

  6. python使用datetime模块计算各种时间间隔的方法

    python使用datetime模块计算各种时间间隔的方法 本文实例讲述了python使用datetime模块计算各种时间间隔的方法.分享给大家供大家参考.具体分析如下: python中通过datet ...

  7. python的datetime模块处理时间

    python的datetime模块主要用来处理时间,里面包含很多类,包括timedelay,date,time,datetime等 开发中经常会用到模块里面的datetime类,这是一个表示日期时间的 ...

  8. python处理时间--- datetime模块

    1   Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块我在之前的文章已经有所介绍,它提供的接口与C标准库time.h基本一致.相比于tim ...

  9. python中datetime模块

    Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块我在之前的文章已经有所介绍,它提供 的接口与C标准库time.h基本一致.相比于time模块 ...

随机推荐

  1. Using pg_dump restore dump file on Odoo

    pg_restore -C -d postgres db.dump

  2. hdu 3348 coins

    这道题算是一道很经典的题,很好的诠释了贪心和动态规划的不同功能.求最少钱的数量用贪心就够了,但是求最多钱的数量要用到动态规划的思想,每步都尽量保留最大 数量.具体看程序注解: #include&quo ...

  3. JavaScript对下一个元旦倒计时,经常用于网店限时销售

    <div>距离下一个元旦还有多久:</div> <div id="timer"></div> <script type=&qu ...

  4. 设计模式(Design Patterns)

    设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...

  5. CodeForces 670E Correct Bracket Sequence Editor(list和迭代器函数模拟)

    E. Correct Bracket Sequence Editor time limit per test 2 seconds memory limit per test 256 megabytes ...

  6. PHP--进行模块化设计

    PHP--进行模块化设计 [来源] 达内    [编辑] 达内   [时间]2012-10-30 导航模块可以简单列为一个关于三级页面链接的HTML文件.通常你可以通过用另一种颜色来标明对当前区域的链 ...

  7. Yii源码阅读笔记(十六)

    Model类,集中整个应用的数据和业务逻辑—— /** * Generates a user friendly attribute label based on the give attribute ...

  8. Worker 工作 后台js 工作

    <script type="text/javascript"> var w; function startWorker() { if (typeof (Worker) ...

  9. word 使用宏批量设置表格

    Sub ChangeTable() Application.Browser.Target = wdBrowseTable To ActiveDocument.Tables.Count ActiveDo ...

  10. A BRIEF HISTORY OF COMPUTERS

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION Vacuum Tubes   Transi ...