python关于时间的函数_python之时间函数

标签: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.

标签:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值