time和datetime模块

time.altzone  #返回与UTC时间的时间差

time.time()  #时间戳,1970年到现在的时间,以秒计算,时间戳可以对时间进行运算。

time.asctime()  #返回时间格式"Sun May 14 13:40:11 2017",不加对象默认为当前时间,如:time.asctime(time.localtime(time.time()+3600*3)),当前时间往后推迟3小时,这种格式一般美国人在用

time.ctime() #返回当前时间‘Sun May 14 14:33:00 2017‘

time.localtime() #返回本地时间 的struct time对象格式,time.struct_time(tm_year=2017, tm_mon=5, tm_mday=14, tm_hour=14, tm_min=3, tm_sec=0, tm_wday=6, tm_yday=134, tm_isdst=0)

        可以进行时间运算,如

        time.localtime(time.time()+3600*3) #往后推迟3小时

time.gmtime() #返回UTC时间的struct time对象格式,time.struct_time(tm_year=2017, tm_mon=5, tm_mday=14, tm_hour=6, tm_min=3, tm_sec=0, tm_wday=6, tm_yday=134, tm_isdst=0)

time.strptime(string,format)#把字符串的时间格式 格式化输出一个struct_time

time.strftime(format,struct_time)  #把struct_time对象格式化输出一个字符串的时间格式

如:

time.strptime(‘2017-5-1 23:59:59‘,‘%Y-%m-%d %H:%M:%S‘) ->time.struct_time(tm_year=2017, tm_mon=5, tm_mday=1, tm_hour=23, tm_min=59, tm_sec=59, tm_wday=0, tm_yday=121, tm_isdst=-1)

time.mktime() #将时间对象转成时间戳

#日期字符串转成时间戳

如:把一个字符串格式为‘2017-5-1 23:59:59‘转换成时间戳

t = time.strptime(‘2017-5-1 23:59:59‘,‘%Y-%m-%d %H:%M:%S‘)   #先把字符串转为为struct-time格式

print(time.mktime(t))  ->1493654399.0                                       #再用mktime函数转换为时间戳

#将时间戳转换为字符串格式

如:

t = time.strptime(‘2017-5-1 23:59:59‘,‘%Y-%m-%d %H:%M:%S‘)

t1_stamp = time.mktime(t)                                                    #得到时间戳

t2 = time.localtime(t1_stamp)           #时间戳转换为struct-time格式

t3 = time.strftime(‘%Y-%m-%d %H-%M-%S‘,t2)  #转换为想要的时间字符串格式

print(t3) ->2017-05-01 23-59-59

时间格式

Directive 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.

datetime模块

时间运算

import datetime
print(datetime.datetime.now()) #返回当前时间2017-05-14 15:50:19.881892
print(datetime.date.fromtimestamp(time.time()) )  # 时间戳直接转成日期格式 2016-08-19,好像没什么用
print(datetime.datetime.now() )
print(datetime.datetime.now() + datetime.timedelta(3)) #当前时间+3天
print(datetime.datetime.now() + datetime.timedelta(-3)) #当前时间-3天
print(datetime.datetime.now() + datetime.timedelta(hours=3)) #当前时间+3小时
print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #当前时间+30分

c_time  = datetime.datetime.now()print(c_time.replace(minute=3,hour=2)) #时间替换
时间: 2024-08-06 07:42:08

time和datetime模块的相关文章

time模块和datetime模块

http://www.cnblogs.com/tkqasn/p/6001134.html 一.time模块 time模块中时间表现的格式主要有三种: a.timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 b.struct_time时间元组,共有九个元素组. c.format time 格式化时间,已格式化的结构使时间更具可读性.包括自定义格式和固定格式. 1.时间格式转换图: 2.主要time生成方法和time格式转换方法实例: import ti

python小白-day6 time&datetime模块

time&datetime ?一.time模块 time模块提供各种操作时间的函数 说明:一般有两种表示时间的方式:       第一种是时间戳的方式(相对于1970.1.1 00:00:00以秒计算的偏移量),时间戳是惟一的       第二种以数组的形式表示即(struct_time),共有九个元素,分别表示,同一个时间戳的struct_time会因为时区不同而不同 1 2 3 4 5 6 7 8 9 10 11 12 import time print('clock():',time.cl

Python的datetime模块分析

datetime模块用于是date和time模块的合集,datetime有两个常量,MAXYEAR和MINYEAR,分别是9999和1. datetime模块定义了5个类,分别是 1.datetime.date:表示日期的类 2.datetime.datetime:表示日期时间的类 3.datetime.time:表示时间的类 4.datetime.timedelta:表示时间间隔,即两个时间点的间隔 5.datetime.tzinfo:时区的相关信息 一.首先看一下datetime.date类

python的datetime模块

导入datetime模块后用dir(datetime)命令查看 在datetime模块下有几个小模块,或叫类 (一)date小模块.处理年月日 也是先用dir(datetime.date)查看一下 其中有那么几个方法 1:today()显示今天的日期 如果嫌这样太麻烦,可以单独导入date这个小模块 2:date小模块是个类,可以对它实例化,可以加个日期参数 这样就可以对这个对象进行操作了 3:year  month day分别取得日期对象中的年月日. 4:weekday(),查看一周中的第几天

20.time与datetime模块

time与datetime模块 时间戳               1970年1月1日之后的秒,即:time.time() 格式化的字符串    2014-11-11 11:11,    即:time.strftime('%Y-%m-%d') 结构化时间          元组包含了:年.日.星期等... time.struct_time    即:time.localtime() import time time.sleep(5) ''' 程序停止5秒 ''' print(time.time(

python time模块和datetime模块详解

一.time模块 time模块中时间表现的格式主要有三种: a.timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 b.struct_time时间元组,共有九个元素组. c.format time 格式化时间,已格式化的结构使时间更具可读性.包括自定义格式和固定格式. 1.时间格式转换图: 2.主要time生成方法和time格式转换方法实例: #! /usr/bin/env python # -*- coding:utf-8 -*- # __auth

python中datetime模块

Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块我在之前的文章已经有所介绍,它提供 的接口与C标准库time.h基本一致.相比于time模块,datetime模块的接口则更直观.更容易调用.今天就来讲讲datetime模块. datetime模块定义了两个常量:datetime.MINYEAR和datetime.MAXYEAR,分别表示datetime所能表示的最 小.最大年份.其中,MINYEAR = 1,MAXYEAR = 9999

python中的datetime模块

Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块我在之前的文章已经有所介绍,它提供的接口与C标准库time.h基本一致.相比于time模块,datetime模块的接口则更直观.更容易调用.今天就来讲讲datetime模块. datetime模块定义了两个常量:datetime.MINYEAR和datetime.MAXYEAR,分别表示datetime所能表示的最小.最大年份.其中,MINYEAR = 1,MAXYEAR = 9999.(

python——从datetime模块探索python的数据架构

问题出现于我试图向自建网页中加入实时时间开始. 我之前已经知道python中有有关事件和日期的模块datetime.以下导入datetime并作实验. >>> import datetime>>> type(datetime) <class 'module'> 可知datetime属于module(模块)类.此外,类似的时间相关模块还有time和calendar. There are two kinds of date and time objects: “

datetime模块

datetime模块包括一些函数和类,用于完成日期和时间的解析.格式化和相关的运行 #!/usr/bin/env python #coding: utf-8 import datetime t = datetime.time(1,2,3) print t #01:02:03 print t.hour    #1 print t.minute  #2 print t.second  #3 print t.microsecond #0 print t.tzinfo  #None print date