Python's Time function

Source: Internet
Author: User
Tags locale month name timedelta

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格式 but returns to local rush time
Print (Time.mktime (Time.localtime ())) #与time. LocalTime () function instead, turn struct_time back to timestamp
#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, returns the current time, but the specified value is replaced
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)


Time parameters and their meanings:

Parameters

Letter Meaning

%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]. A New year preceding the first Sunday is considered to is 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]. A New year preceding the first Monday is considered to is 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 repres Ents 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's Time function

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.