(Python basis) time, time tuple localtime, time formatting strftime

Source: Internet
Author: User
Tags local time month name

You can run the following code directly to view the results:

#!/usr/bin/python
# Coding=utf-8
Import Time # Introducing the time module

# time Stamp:
# each timestamp is represented by how long it has been since midnight on January 1, 1970 (calendar).
# but the dates before 1970 cannot be expressed in this way. Too far away from the date, UNIX and Windows only support to 2038 years.
# Python has many functions under the time module to convert common date formats. such as the function Time.time () is used to get the current timestamp, as in the following instance:
ticks = time.time ()
print ("Current timestamp:%f"% (ticks))

# Get current time
# Tm_year | 4-digit year: 2008
# Tm_mon | Months: 1 to 12
# Tm_mday | Days: 1 to 31
# Tm_hour | Hours: 0 to 23
# Tm_min | Minutes: 0 to 59
# Tm_sec | Seconds: 0 to 61 (60 or 61 is leap seconds)
# Tm_wday | Day of the week: 0 to 6 (0 is Monday)
# Tm_yday | Day of the year: 1 to 366
# TM_ISDST | Daylight Saving Time: 1 (Daylight saving Time), 0 (not daylight saving time), 1 (unknown), Default-1
# Converts from the time of the return floating-point number to the time tuple, as long as the floating-point number is passed to a function such as localtime.
localtime = Time.localtime (Time.time ())
print ("local time:%s"% (localtime))

# Formatted Date: Using the strftime of the time module
#%y Two-digit year representation (00-99)
#%Y Four-digit year representation (000-9999)
#%m Month (01-12)
#%d months of the day (0-31)
#%H 24-hour hours (0-23)
#%I 12-hour hours (01-12)
#%M minutes (00=59)
#%s seconds (00-59)
#%a Local Simplified week name
#%A Local Full week name
#%b a locally simplified month name
#%B Local Full month name
#%c Local corresponding date representation and time representation
#%j Day of the Year (001-366)
#%p the equivalent of a local a.m. or p.m.
#%u week of the Year (00-53) Sunday is the beginning of the week
#%w Week (0-6), Sunday for the beginning of the week
#%W weeks of the year (00-53) Monday is the beginning of the week
#%x Local corresponding date representation
#%x local corresponding time representation
#%Z The name of the current time zone
#% percent of the number itself
# Formatted as 2016-03-20 11:45:39 form
Print (Time.strftime ("%y-%m-%d%h:%m:%s", Time.localtime ()))
# formatted as Sat 28 22:24:24 2016 form
Print (Time.strftime ("%a%b%d%h:%m:%s%Y", Time.localtime ()))
# Convert a format string to a timestamp: Time.mktime (tupletime) accepts a time tuple and returns the time suffix (number of floating-point seconds after the 1970 era)
a = "Sat Mar 22:24:24"
Print (Time.mktime (Time.strptime (A, "%a%b%d%h:%m:%s%Y" )))

(Python basis) time, time tuple localtime, time formatting strftime

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.