Python formatted date

Source: Internet
Author: User

The usual time functions are as follows

Get Current Date: Time.time ()

Get the timestamp in the form of a tuple: time.local (Time.time ())

Functions formatted with dates (formatted as tuple-based):

(1) Time.asctime (time.local (Time.time ()))

(2) Time.strftime (Format[,t])

To convert a format string to a timestamp:

Time.strptime (str,fmt= '%a%b%d%h:%m:%s%Y ')

Deferred execution: Time.sleep ([secs]), in seconds

Example 1:

#-*-coding:utf-8-*-Import Time#Current TimePrinttime.time ()#time Stamp FormPrintTime.localtime (Time.time ())#easy-to-read formPrintTime.asctime (Time.localtime (Time.time ()))#formatted as 2016-03-20 11:45:39 in the form ofPrintTime.strftime ("%y-%m-%d%h:%m:%s", Time.localtime ())#formatted as Sat 28 22:24:24 2016 FormPrintTime.strftime ("%a%b%d%h:%m:%s%Y", Time.localtime ())#convert a format string to a timestampA ="Sat Mar 22:24:24"PrintTime.mktime (Time.strptime (A,"%a%b%d%h:%m:%s%Y"))

Output:

1481036968.19time.struct_time (tm_year=2016, tm_mon=12, tm_mday=6, tm_hour=23, tm_min=9, tm_sec=28, tm_wday= 1, tm_yday=341, tm_isdst=23:09:28 20162016-12-06 23:09:2806 23:09:28 20161459175064.0

Example 2: A time is compared with the current, if more than the current time to call a script, or wait half a child to continue to judge

#-*-coding:utf-8-*-Import TimeImportSYSImportOS#determine if the current time exceeds an input timedefFuctime (s):ifTime.strftime ('%y-%m-%d%h:%m:%s', Time.localtime (Time.time ())) >S:returnTrueElse:        returnFalse while(1):    ifFuctime ('2016-12-05 00:00:00'):        #An easy way to invoke a script under a pathOs.system ("python./.. /day_2/prime.py./... /day_2/inti_prime.txt./... /day_2/res_prime.txt")         Break    Else: Time.sleep (1800)        Continue

Python formatted date

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.