Python Time processing

Source: Internet
Author: User

When processing data and machine learning, there are a number of time series that need to be processed. For example, the database reads the STR and time conversions, and the time difference is calculated. Summarize Python's time-processing content and hope to help you learn python.conversion of a string and time series·time.strptime (): string = Time series ·time.strftime (): Time Series = string Import TimeStart = "2017-01-01" end = "2017-8-12" StartTime = Time.strptime (Start, "%y-%m-%d") EndTime = Time.strptime (end, "%y-%m-%d" ) # The second parameter format specifies formattingPrint(StartTime);Print(endTime) _start = Time.strftime ('%y%m%d-%h:%m:%s ', startTime) _end = Time.strftime ('%y%m%d-%h:%m:%s ', endTime) # The parameter position is exactly the oppositePrint(_start)Print(_end)second, time stamp·time.mktime (t): Converts t to timestamp ·Time.localtime (s): Converts timestamp to time Startstamp = Time. Mktime (StartTime) # Time= time stamp Endstamp = Time. Mktime (endTime) print (startstamp) print (endstamp) _starttime = Time. LocalTime (startstamp) # timestamp = TimePrint (_starttime)third, date arithmeticNote: The timestamp is starting from 1970, in seconds, so our calculations can be based on the number of secondsFor example, here I calculate the number of days between StartTime and EndTime: print (int((Endstamp-startstamp)/(24*60*60)))Iv. Summary of the format%a English week abbreviation%a English week full%b English month abbreviation%b English month full%c show local datetime%d date, take 1-31%h hour, 0-23%i Hours, 0-12%m months, 01-12%m minutes, 1-59%j days of the day%w show today is the day of the week%w first week%x date%x local day time%y year 00-99 full spelling of%y yearSource: Blog Park

Python Time processing

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.