Python Learning (20) Time module method

Source: Internet
Author: User

1. Time Module
  How many seconds have the timestamp gone from the beginning of Unix to now?
Formatted time
1) Get the current timestamp
Import  Time Print (Time.time ()) # gets the current time stamp
2) output after a period of time
Import Timetime.sleep (ten) Print ('haha')  # Print after 10 seconds
3) Get a good time to format
Import timetoday=time.strftime ('%y-%m-%d%h:%m:%s'#  Get a well-formatted time print(today)
4) Default time to get the standard timezone
Import  Time Print (Time.gmtime ())
5) Get the time of the current time zone
Import  Time Print # time to take the current time zone
6) The timestamp is converted to a time tuple, and then the time tuple is converted to a formatted time
Import  Times # Convert a timestamp to a time tuple Print (Time.strftime ('%y-%m-%d%h:%m:%s'# Converts the time tuple to a formatted time
7) Returns the current formatted time by default, and if a timestamp is passed, converts the timestamp to a formatted time and returns
Import  Time
def Timestamp_to_fomat (timestamp=none,format='%y-%m-%d%h:%m:%s'):     if  timestamp:        time_tuple=time.localtime (timestamp)        res=  Time.strftime (format,time_tuple)    Else:        res=time.strftime (format)     return Res Print (Timestamp_to_fomat ()) Print (Timestamp_to_fomat (1514198608))
8) Convert the formatted time to a time tuple, and then convert the time tuple to a timestamp
Import timetp=time.strptime ('2018-4-21','%y-%m-%d') # convert formatted time to time tuple Print # convert a time tuple to a timestamp
9) The function does not pass arguments, returns the timestamp of the current time, and the passed-in parameter returns the timestamp of the parameter
Import TimedefStrtimestamp (str=none,format='%y%m%d%h%m%s'):    ifSTR:TP=time.strptime (Str,format)#Convert to a time tupleRes=time.mktime (TP)#convert to time stamp again    Else: Res=time.time ()#Timestamp of the default fetch current time    returnInt (res)Print(Strtimestamp ())Print(Strtimestamp ('20181229183859'))Print(Strtimestamp ('2018-12-29','%y-%m-%d'))
2. DateTime Module 1) Get the current time
Import datetime Print # Get current Time
2) Get the time of day, accurate to the day
Import datetime Print # accurate to the day
3) Get the time after a few days
Import Datetimeres # get to 5 days in time Print (res)    # can also write minutes,weeks,seconds   time
4) Get to a few days before the time
Import Datetimeres # get to 5 days ago Print (RES)
5) Get a few days before the time (output format customization)
Import Datetimeres # get to 5 days ago Print (Res.strftime ('%y%m%d'))

Python Learning (20) Time module method

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.