time 模块

time模块方法:

>>> import time
>>> time.time() #时间戳 秒级别
1519212085.6211221 #从1970年到现在一共度过1519212085秒;
>>> time.time()/3600/24/365 #48年 1970-2018年有这么多秒 1h=3600s
48.17390346104816
>>> time.localtime() #本地时间
time.struct_time(tm_year=2018, tm_mon=2, tm_mday=21, tm_hour=20, tm_min=8, tm_sec=33, tm_wday=2, tm_yday=52, tm_isdst=0)
>>> time.localtime(2342342434)
time.struct_time(tm_year=2044, tm_mon=3, tm_mday=23, tm_hour=18, tm_min=40, tm_sec=34, tm_wday=2, tm_yday=83, tm_isdst=0)
>>> a=time.localtime()
>>> a
time.struct_time(tm_year=2018, tm_mon=2, tm_mday=21, tm_hour=19, tm_min=28, tm_sec=45, tm_wday=2, tm_yday=52, tm_isdst=0)
>>> '%s-%s-%d'%(a.tm_year,a.tm_mon,a.tm_mday)
'2018-2-21'
>>> time.mktime(a) #把localtime()时间反转时间成时间戳
1519214861.0
0  tm_year    #年 1970-2018
1 tm_mon #月 1-12
2 tm_mday #日 1-31
3 tm_hour #时 0-23
4 tm_min #分 0-59
5 tm_sec #秒 0-59
6 tm_wday #一周中得第几天 0-6
7 tm_yday #一年中得第几天 0-365
8 tm_isdst #是否是夏令时 0-1
>>> time.gmtime()     #比北京时间晚8点 UTC时区(0时区)
time.struct_time(tm_year=2018, tm_mon=2, tm_mday=21, tm_hour=11, tm_min=50, tm_sec=46, tm_wday=2, tm_yday=52, tm_isdst=0)
>>> time.sleep(2) #睡一会 单位为 秒
>>> time.asctime() #表示时间得另外一种方法
'Wed Feb 21 20:53:43 2018'
>>> time.ctime() #表示时间得另外一种方法
'Wed Feb 21 20:57:16 2018'
>>> time.ctime(3123)
'Thu Jan 1 08:52:03 1970'
>>> time.ctime(0)
'Thu Jan 1 08:00:00 1970'
>>> time.strftime('%Y-%m-%d %H:%M:%S') #表示时间 字符串
'2018-02-21 21:04:48'
>>> a=time.localtime(2432423423)
>>> a
time.struct_time(tm_year=2047, tm_mon=1, tm_mday=30, tm_hour=9, tm_min=10, tm_sec=23, tm_wday=2, tm_yday=30, tm_isdst=0)
>>> time.strftime('%Y-%m-%d %H:%M:%S',a) #表示某一时间戳得 字符串
'2047-01-30 09:10:23'
>>> time.strftime('%Y-%m-%d %H:%M:%S %a',a) #a 周几
'2047-01-30 09:10:23 Wed'
>>> time.strftime('%Y-%m-%d %H:%M:%S %A',a) #A 周几
'2047-01-30 09:10:23 Wednesday'
>>> time.strftime('%y-%m-%d %H:%M:%S %b') #b 月
'18-02-21 21:22:19 Feb'
>>> time.strftime('%y-%m-%d %H:%M:%S %B') #B 月
'18-02-21 21:22:29 February'
>>> time.strftime('%Y-%m-%d %H:%M:%S %p',a) #p 上午下午
'2047-01-30 09:10:23 AM'
>>> time.strftime('%Y-%m-%d %H:%M:%S %U') #U 一年得第几周
'2018-02-21 21:08:15 07'
>>> time.strftime('%Y-%m-%d %H:%M:%S %w') #w 一周得第几天
'2018-02-21 21:09:55 3'
>>> time.strftime('%y-%m-%d %H:%M:%S %z') #z 东8区时间
'18-02-21 21:10:38 +0800'
>> time.strftime('%Y-%m-%d %H:%M:%S %Z') #Z 时间标准
'2018-02-21 21:28:22 中国标准时间' >>> s=time.strftime('%Y-%m-%d %H:%M:%S') #时间字符串 格式 时间戳来回转换
>>> s
'2018-02-21 21:32:19'
>>> s2= time.strptime(s,'%Y-%m-%d %H:%M:%S')
>>> s2
time.struct_time(tm_year=2018, tm_mon=2, tm_mday=21, tm_hour=21, tm_min=32, tm_sec=19, tm_wday=2, tm_yday=52, tm_isdst=-1)
>>> time.mktime(s2)
1519219939.0
字符串转时间格式对应表
Meaning Notes
%a Locale’s abbreviated weekday name.
%A Locale’s full weekday name.
%b Locale’s abbreviated month name.
%B Locale’s full month name.
%c Locale’s appropriate date and time representation.
%d Day of the month as a decimal number [01,31].
%H Hour (24-hour clock) as a decimal number [00,23].
%I Hour (12-hour clock) as a decimal number [01,12].
%j Day of the year as a decimal number [001,366].
%m Month as a decimal number [01,12].
%M Minute as a decimal number [00,59].
%p Locale’s equivalent of either AM or PM. (1)
%S Second as a decimal number [00,61]. (2)
%U Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0. (3)
%w Weekday as a decimal number [0(Sunday),6].
%W Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0. (3)
%x Locale’s appropriate date representation.
%X Locale’s appropriate time representation.
%y Year without century as a decimal number [00,99].
%Y Year with century as a decimal number.
%z Time zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59].
%Z Time zone name (no characters if no time zone exists).
%% A literal '%' character.

--------------------------------------

总结:time模块
1.time.time()
2.time.localtime()
3.time.localtime(2342342434)
4.time.mktime(a)
5.time.gmtime()
6.time.sleep(2)
7.time.asctime()
8.time.ctime()
9.time.ctime(3123)
10.time.strftime('%Y-%m-%d %H:%M:%S')
11.time.strftime('%Y-%m-%d %H:%M:%S',a)
12.time.strftime('%Y-%m-%d %H:%M:%S %A',a)
13.time.strptime(s,'%Y-%m-%d %H:%M:%S')

-----------------------------------------------------------

datetime模块

相比于time模块 datetime模块得接口更直观更容易调用 重点是进行时间运算

方法:
>>> a=datetime.datetime.now()
>>> a
datetime.datetime(2018, 2, 21, 21, 59, 57, 526811)
>>> a.year
2018
>>> a.timetuple()
time.struct_time(tm_year=2018, tm_mon=2, tm_mday=21, tm_hour=21, tm_min=59, tm_sec=57, tm_wday=2, tm_yday=52, tm_isdst=-1)
>>> d2=datetime.date.fromtimestamp(time.time()) #根据时间戳快速拿到年月日
>>> d2
datetime.date(2018, 2, 21)
>>> d2.timetuple() #注意 时分秒 丢了
time.struct_time(tm_year=2018, tm_mon=2, tm_mday=21, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=52, tm_isdst=-1) 时间运算:
>>> datetime.datetime.now()-datetime.timedelta(days=1)
datetime.datetime(2018, 2, 20, 22, 13, 4, 891830)
>>> datetime.datetime.now()-datetime.timedelta(days=3)
datetime.datetime(2018, 2, 18, 22, 14, 7, 771268)
>>> datetime.datetime.now()-datetime.timedelta(hours=3)
datetime.datetime(2018, 2, 21, 19, 14, 33, 758609)
>>> datetime.datetime.now()+datetime.timedelta(hours=3)
datetime.datetime(2018, 2, 22, 1, 14, 48, 426850)
>>> datetime.datetime.now()+datetime.timedelta(minutes=10)
datetime.datetime(2018, 2, 21, 22, 25, 32, 615892)
>>> datetime.datetime.now()+datetime.timedelta(seconds=10)
datetime.datetime(2018, 2, 21, 22, 16, 29, 661140) 时间替换:
>>> s=datetime.datetime.now()
>>> s
datetime.datetime(2018, 2, 21, 22, 21, 34, 62949)
>>> s.replace(year=2016)
datetime.datetime(2016, 2, 21, 22, 21, 34, 62949)
>>> s.replace(year=2016,month=8)
datetime.datetime(2016, 8, 21, 22, 21, 34, 62949)
>>> s.replace(year=2016,month=8,day=2)
datetime.datetime(2016, 8, 2, 22, 21, 34, 62949)

总结:
1.方法:a=datetime.datetime.now() a.year a.timetuple() d2=datetime.date.fromtimestamp(time.time())
2.时间运算:datetime.datetime.now()-datetime.timedelta(days=3)
3.时间替换:s=datetime.datetime.now() s.replace(year=2016,month=8)

补充:

#把datetime转成字符串
def datetime_toString(dt):
return dt.strftime("%Y-%m-%d-%H") #把字符串转成datetime
def string_toDatetime(string):
return datetime.strptime(string, "%Y-%m-%d-%H") #把字符串转成时间戳形式
def string_toTimestamp(strTime):
return time.mktime(string_toDatetime(strTime).timetuple()) #把时间戳转成字符串形式
def timestamp_toString(stamp):
return time.strftime("%Y-%m-%d-%H", tiem.localtime(stamp)) #把datetime类型转外时间戳形式
def datetime_toTimestamp(dateTim):
return time.mktime(dateTim.timetuple())
#coding=utf-8
import time
import datetime def yes_time():
#获取当前时间
now_time = datetime.datetime.now()
#当前时间减去一天 获得昨天当前时间
yes_time = now_time + datetime.timedelta(days=-1)
#格式化输出
yes_time_str = yes_time.strftime('%Y-%m-%d %H:%M:%S')
print yes_time_str # 2017-11-01 22:56:02 def dif_time():
#计算两个时间之间差值
now_time = datetime.datetime.now()
now_time = now_time.strftime('%Y-%m-%d %H:%M:%S')
d1 = datetime.datetime.strptime('2017-10-16 19:21:22', '%Y-%m-%d %H:%M:%S')
d2 = datetime.datetime.strptime(now_time, '%Y-%m-%d %H:%M:%S')
#间隔天数
day = (d2 - d1).days
#间隔秒数
second = (d2 - d1).seconds
print day #
print second #13475 注意这样计算出的秒数只有小时之后的计算额 也就是不包含天之间差数 def unix_time():
#将python的datetime转换为unix时间戳
dtime = datetime.datetime.now()
un_time = time.mktime(dtime.timetuple())
print un_time #1509636609.0
#将unix时间戳转换为python 的datetime
unix_ts = 1509636585.0
times = datetime.datetime.fromtimestamp(unix_ts)
print times #2017-11-02 23:29:45

模块 - time/datetime的更多相关文章

  1. Python常用模块(time, datetime, random, os, sys, hashlib)

    time模块 在Python中,通常有这几种方式来表示时间: 时间戳(timestamp) :         通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量.我们运 ...

  2. python3 time模块与datetime模块

    time模块 在Python中,通常有这几种方式来表示时间:1)时间戳 2)格式化的时间字符串 3)元组(struct_time)共九个元素.由于Python的time模块实现主要调用C库,所以各个平 ...

  3. Time模块和datetime模块

    Time模块和datetime模块 一. 调用 import time       #调用time模块 二.使用方法 1.time.time 拿到时间戳.以Linux诞生年份1970年开始计算到程序执 ...

  4. python 时间模块(time ,datetime,calendar)

    Python中提供了时间相关的内置模块,我们主要用的是:time模块.datetime模块和calendar模块 ⽇期格式化的标准: %y 两位数的年份表示(00-99) %Y 四位数的年份表示(00 ...

  5. Python常用模块time & datetime &random 模块

    时间模块前言 在Python中,与时间处理有关的模块就包括:time,datetime 一.在Python中,通常有这几种方式来表示时间: 时间戳 格式化的时间字符串 元组(struct_time)共 ...

  6. python中time模块和datetime模块

    time模块和datetime模块 时间分为三种模式(time 模块) 时间戳   (time.time()) 格式化字符串 (time.strftime(%Y-%m-%d %H:%M:%S %p)) ...

  7. Python之路(第十六篇)xml模块、datetime模块

    一.xml模块 xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单, xml比较早,早期许多软件都是用xml,至今很多传统公司如金融行业的很多系统的接口还主要 ...

  8. python內建模块之datetime

    from:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00143193755 ...

  9. Python模块学习 ---- datetime

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

  10. (转)python time模块和datetime模块详解

    python time模块和datetime模块详解 原文:http://www.cnblogs.com/tkqasn/p/6001134.html 一.time模块 time模块中时间表现的格式主要 ...

随机推荐

  1. iOS多线程与网络开发之NSURLCache

    郝萌主倾心贡献,尊重作者的劳动成果.请勿转载. // 2 // ViewController.m 3 // NSURLCacheDemo 4 // 5 // Created by haomengzhu ...

  2. 斑马Zebra ZPLII指令集中文说明解释

      我们最常用的斑马(Zebra)条码打印机,应用ZPLII命令来控制打印,说明书中有每条指令的详细说明及相关示例,下面是各指令的中文释义: ^A 对Zebra内置点阵字体缩放 ^A(可缩放/点阵字体 ...

  3. 折腾gcc/g++链接时.o文件及库的顺序问题(转)

    转自: http://www.cnblogs.com/OCaml/archive/2012/06/18/2554086.html#sec-1-1 折腾gcc/g++链接时.o文件及库的顺序问题 Tab ...

  4. Thrall’s Dream 第四届山东省省赛 (直接暴力DFS)

    题目链接:题目 AC代码: #include<iostream> #include<algorithm> #include<vector> #include< ...

  5. Oracle连接远程数据库的四种设置方法

    Oracle数据库的远程连接可以通过多种方式来实现,本文我们主要介绍四种远程连接的方法和注意事项,并通过示例来说明,接下来我们就开始介绍 第一种方法: 若oracle服务器装在本机上,那就不多说了,连 ...

  6. BodyTagSupport小案例1

    做了个简单的实验:写一个tag,将tag body中的内容打印成一个三角形 代码很简单就不赘述了,直接贴在下面,值得注意的是这个图(摘自李兴华JAVA开发实战经典) 在做的过程中遇到了如下问题: 1. ...

  7. Spring中的IOC

    在学习spring的时候,最常听到的词应该就是IOC和AOP了,以下,我从我的角度再次理解一下Spring里的IOC和AOP. IOC简单介绍 IoC(InversionofControl):IoC就 ...

  8. 线程池 (thread pool) 的类型与实现方式

    在许多应用中需要频繁的创建许多生命周期很短的线程,如果用传统方法的话就会造成大量的资源了浪费,java的设计者们考虑到了这点在java中加入了线程池这个特性,它负责管理大量的线程的创建销毁等操作. 首 ...

  9. AJax与Jsonp跨域访问

    一.JavaScript的AJax AJAX即"Asynchronous Javascript And XML"(异步JavaScript和XML) 设计AJax使用的一种重要技术 ...

  10. javascript之查找数组元素

    基本思想: 比对数组中元素,相等者输出元素在数组的下标,否则就输出没找到! 代码如下: function Orderseach(array,findVal){ var temp = false; // ...