码迷,mamicode.com
首页 > 编程语言 > 详细

python之时间函数

时间:2016-05-25 20:28:09      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:

import time

print(time.clock())
print(time.process_time())
print(time.time()) #返回当前系统时间戳
print(time.ctime()) #返回当前系统时间
print(time.ctime(time.time()-86640)) #将时间戳转为字符串
print(time.gmtime(time.time()-86640)) #将时间戳转为struct_time格式
print(time.localtime(time.time()-86640)) #将时间戳转为struct_time格式,但返回本地赶时间
print(time.mktime(time.localtime())) #与time.localtime()功能相反,将struct_time转回时间戳
#time.sleep(1) #睡眠4秒
print(time.strftime("%Y-%m-%d %H:%M:%S"),time.gmtime())#将struct_time转成指定字符串格式
print(time.strptime("2016-01-28","%Y-%m-%d")) #字符串格式转成struct_time

import datetime

print(datetime.date.today()) #输出格式 2016-05-25
print(datetime.date.fromtimestamp(time.time() - 864400)) #将时间戳转成日期格式
current_time = datetime.datetime.now()
print(current_time) #输出2016-05-25 19:33:49.521486
print(current_time.timetuple()) #返回struct_time格式
print(current_time.replace(2014,9,12)) #输出2014-09-12 19:06:24.074900,返回当前时间,但指定的值将被替换
str_to_date = datetime.datetime.strptime("2016-05-25 19:42:00","%Y-%m-%d %H:%M:%S") #将字符串转换成日期格式
new_date = datetime.datetime.now() + datetime.timedelta(days=10)
new_date = datetime.datetime.now() + datetime.timedelta(days=-10)
new_date = datetime.datetime.now() + datetime.timedelta(hours=-10)
new_date = datetime.datetime.now() + datetime.timedelta(seconds=120)


时间参数及其函义:

 

参数

函义

   

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

%S

Second as a decimal number [00,61].

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

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

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

 



python之时间函数

标签:

原文地址:http://www.cnblogs.com/kongzhagen/p/5528295.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!