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. Repeater控件中的三目运算

    <asp:Repeater ID="rptimg" runat="server">        <ItemTemplate>      ...

  2. JAVA操作COOKIE

    JAVA操作COOKIE 1.设置Cookie Cookie cookie = new Cookie("key", "value"); cookie.setMa ...

  3. 从prompt输入10个人的年龄放入数组,将十个人的年龄求总和。

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. salt-ssh使用

    salt-ssh 是 0.17.0 新出现的一个功能,一听这名字就知道它是依赖 ssh 来进行远程命令执行的工具,好处就是你不需要在客户端安装 minion,也不需要安装 master(直接安装 sa ...

  5. POJ 2528 Mayor's posters(线段树区间染色+离散化或倒序更新)

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 59239   Accepted: 17157 ...

  6. [英语学习]国外的在线广播网站,类似喜马拉雅和荔枝FM

    今天在Seattle Art  Museum 网站上看东西,发现了这个好网站. 主要是外文资料,可以练听力,也可以找到<我爱吕西安>的英文版本. https://soundcloud.co ...

  7. [听课笔记]Professor Michael Cusumano's New Book:" Strategy Rules: Five Timeless Lessons from Bill Gates, Andy Grove, and Steve Jobs"

    1. Look Forward, Reason Back Extrapolate, interpret, then tie vision to concrete actions2. Make Big ...

  8. python 操作excel 使用笔记

    写入excel, 保存的过程中需要注意,保存格式xls后缀,如果用xlsx会报错 def set_style(name,height,bold=False): """&q ...

  9. Yii源码阅读笔记(四)

    所有控制器action的基类yii\base\Action.php namespace yii\base;//定义的命名空间 use Yii //使用的命名空间 class Action extend ...

  10. terminal(终端),shell,tty,console(控制台)区别

    原文地址  stackexchange:What is the exact difference between a 'terminal', a 'shell', a 'tty' and a 'con ...