time和datetime时间戳---python


time模块

 

time模块提供各种操作时间的函数

说明:一般有两种表示时间的方式:
       1.时间戳的方式(相对于1970.1.1 00:00:00以秒计算的偏移量),时间戳是惟一的

       2.以数组的形式表示即(struct_time),共有九个元素,分别表示,同一个时间戳的struct_time会因为时区不同而不同

The tuple items are:
  year (including century, e.g. 1998)
  month (1-12)
  day (1-31)
  hours (0-23)
  minutes (0-59)
  seconds (0-59)
  weekday (0-6, Monday is 0)
  Julian day (day in the year, 1-366)
  DST (Daylight Savings Time) flag (-1, 0 or 1)

<span style="font-family:FangSong_GB2312;font-size:18px;">
  time()  -- 返回时间戳
  sleep() -- 延迟运行单位为s
  gmtime() -- 转换时间戳为时间元组(时间元组)
  localtime() -- 转换时间戳为本地时间对象
  asctime() -- 将时间对象转换为字符串
  ctime() -- 将使劲按戳转换为字符串
  mktime() -- 将本地时间转换为时间戳
  strftime() -- 将时间对象转换为规范性字符串

</span>


常用的格式代码:



%Y Year with century as a decimal number.
%m Month as a decimal number [01,12].
%d Day of the month as a decimal number [01,31].
%H Hour (24-hour clock) as a decimal number [00,23].
%M Minute as a decimal number [00,59].
%S Second as a decimal number [00,61].
striptime() --将时间字符串根据指定的格式化字符转换成数组形式的时间

print(time.time())  ---返回当前时间戳
print(time.ctime())  ---返回当前时间
print(time.ctime(time.time()-86400)) --将时间戳转换为字符串
print(time.localtime(time.time()-86400)) --本地时间
print(time.mktime(time.localtime()))  --与time.localtime()功能相反,将struct_time格式转回成时间戳格式
print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()))  -- 将struct_time格式转换指定d字符串格式
print(time.strptime("2016-01-28","%Y-%m-%d"))  --将字符串格式转换成struct_time格式
time.sleep(5) 休眠5s


datetime 模块
定义的类有:


datetime 模块
定义的类有:
datetime.date   --表示日期的类。常用的属性有year,month,day
datetime.time   ---表示时间的类。床用的属性有hour,minute,second,microsecond
datetime.datetime  --表示日期时间
datetime.timedalta --表示时间间隔,即两个时间点之间的长度
date类
date类表示日期,构造函数如下:
datetime.date(year,month,day);
	year(1-9999)
	month(1-12)
	day(1-31)
date.today()--返回一个表示当前本地日期的date对象
date.fromtimestamp(timestamp) -- 根据给定的时间戳,返回一个date对象
date.year() -- 取给定时间的年
date.month() -- 取时间对象的月
date.day() -- 取给定时间的日
	date.replace()  -- 生成一个新的日期对象,用参数指定的年,月, 日代替原有对象中的属性
	date.timetuple() -- 返回日期对应的time.struct_time对象
	date.weekday() -- 返回week,Monday==0...Sunday == 6
	date.isoweekday() -- 返回weekday,Monday == 1...Sunday == 7
	date.ctime()  -- 返回给定时间的字符串格式

print(datetime.date.today().year)  -- 取时间对象的年
print(datetime.date.today().month) --取时间对象的月
print(datetime.date.today().day)  --取时间对象的日
print(datetime.date.today().replace(2010,6,12))  --生成一个新的日期对象,用参数指定的年,月,日代替原有对象中的属性
print(datetime.date.today().timetuple()) 返回给定时间的时间元组/对象
print(datetime.date.today().weekday())  -- 返回weekday,从0开始
print(datetime.date.today().ctime)  --返回给定时间的字符串格格式


	.tiem类
	time类表示时间,由时,分,秒以及微秒组成
		time.min()  --最小表示时间
		time.max()  --最大表示时间
		time.resolution()  -- 微秒
	案例:
		最大时间
		print(datetime.time.max)
		最小时间
		print(datetime.time.min)
		时间最小单位,微秒
		print(datetime.time.resolution)

	·datetime类
		datetime是date与time的结合体,包括date与time的所有信息
		datetime.max()  --最大值
		datetime.min()  --最小值
		datetime.resolution --datetime最小单位
		datetime.today()  -- 返回一个表示当前本地时间
		datetime.fromtimestamp()  --根据给定的时间戳,返回一个datetime对象
		datetime.year() --取年
		datetime.month() --取月
		datetime.day() -- 取日期
		datetim.replace() - 替换时间
		datetime.strptime()  --将字符串转换成日期格式
		datetime.time() -- 取给定日期时间的时间
案例:


print(datetime.datetime.max)         #datetime最大值

print(datetime.datetime.min)         #datetime最小值

print(datetime.datetime.resolution)   #datetime最小单位

print(datetime.datetime.today())     #返回一个表示当前本地时间

print(datetime.datetime.fromtimestamp(time.time()))#根据给定的时间戮,返回一个datetime对象

print(datetime.datetime.now().year)   #取时间对象的年

print(datetime.datetime.now().month)   #取时间对象的月

print(datetime.datetime.now().day)    #取时间对象的日

print(datetime.datetime.now().replace(2010,6,12))#生成一个新的日期对象,用参数指定的年,月,日代替原有对象中的属性

print(datetime.datetime.now().timetuple())  #返回给定时间的时间元组/对象

print(datetime.datetime.now().weekday())  #返回weekday,从0开始

print(datetime.datetime.now().isoweekday())  #返回weekday,从1开始

print(datetime.datetime.now().ctime())    #返回给定时间的字符串格式

print(datetime.datetime.strptime("21/11/06 16:30", "%d/%m/%y %H:%M"))#将字符串转换成日期格式

print(datetime.datetime.now().time())   #取给定日期时间的时间

print(datetime.datetime.now() + datetime.timedelta(days=-5))#获取5日前时间

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值