Python Date and time

Source: Internet
Author: User
Tags current time local time month name readable in python
Python Date and time

Python programs can handle dates and times in many ways, and converting date formats is a common feature.

Python provides a time and calendar module that can be used to format dates and times.

The interval is a floating-point decimal in seconds.

Each timestamp is represented by how long it has been since midnight, January 1, 1970 (calendar).

There are many functions under the Python time module that can transform common date formats. For example, the function Time.time () is used to get the current timestamp, as follows:

#!/usr/bin/python
#-*-coding:utf-8-*-

import time;  # Introduce time module

ticks = Time.time ()
print "Current timestamp:", ticks

The above example outputs the result:

Current timestamp is: 1459994552.51

Timestamp units are best suited for date operations. But the date before 1970 cannot be expressed as such. Too remote a date, UNIX and Windows only support up to 2038 years.
What is a time tuple.

Many Python functions use a single element to assemble 9 of digital processing times:

Serial Number Field value
0 4 digits a few years 2008
1 Month 1 to 12
2 Day 1 to 31
3 Hours 0 to 23
4 Minutes 0 to 59
5 Seconds 0 to 61 (60 or 61 is leap seconds)
6 The day ordinal of a week 0 to 6 (0 is Monday)
7 The first day of the year 1 to 366 (Julian calendar)
8 Daylight saving time -1, 0, 1,-1 is the flag to decide whether to be daylight saving time

This is the struct_time tuple. This structure has the following properties:

Serial Number Property value
0 Tm_year 2008
1 Tm_mon 1 to 12
2 Tm_mday 1 to 31
3 Tm_hour 0 to 23
4 Tm_min 0 to 59
5 Tm_sec 0 to 61 (60 or 61 is leap seconds)
6 Tm_wday 0 to 6 (0 is Monday)
7 Tm_yday 1 to 366 (Julian calendar)
8 Tm_isdst -1, 0, 1,-1 is the flag to decide whether to be daylight saving time

Get current Time

Converts to a time tuple from the time the floating-point number is returned, as long as the floating-point number is passed to functions such as localtime.

#!/usr/bin/python
#-*-coding:utf-8-*-

import time

localtime = Time.localtime (Time.time ())
print " local time is: ", localtime

The above example outputs the result:

local time: Time.struct_time (tm_year=2016, tm_mon=4, tm_mday=7, tm_hour=10, tm_min=3, tm_sec=27, tm_wday=3, tm_yday=98, tm_ isdst=0)

get the formatted time

You can choose a variety of formats according to your requirements, but the simplest function to get the readable time pattern is asctime ():

#!/usr/bin/python
#-*-coding:utf-8-*-

import time

localtime = Time.asctime (Time.localtime () )
print "local time is:", localtime

The above example outputs the result:

local time: Thu APR  7 10:05:21 2016
Format Date

We can use the Strftime method of the time module to format the date:

Time.strftime (format[, T])
#!/usr/bin/python
#-*-coding:utf-8-*-

import time

# formatted into 2016-03-20 11:45:39 form
Print Time.strftime ( "%y-%m-%d%h:%m:%s", Time.localtime ()) 

# formatted into the SAT 28 22:24:24 2016 form
print time.strftime ("%a%b%d%h:%m:%s%Y"  , Time.localtime ()) 
  
# Converts the format string to timestamp
a = "Sat Mar 22:24:24 2016"
Print Time.mktime (Time.strptime (A, "%a%b%d %h:%m:%s%Y "))

The above example outputs the result:

2016-04-07 10:25:09
Thu Apr 10:25:09 2016
1459175064.0

Time date format symbol in Python:%y Two-digit year representation (00-99)%y four-digit year (000-9999)%m month (01-12)%d month (0-31)%H 24-hour hours (0-23)%I 12 hours (01-12)%m minutes (00=59)%s seconds (00-59)%a Local simplified week name%a Local full week name%b locally simplified month name%b Local full month name%c local corresponding date representation and time for%j year (001 -366)%p local a.m. or p.m. The number of weeks in the year (00-53) Sunday is the beginning of the week%w Week (0-6), Sunday is the beginning of the week%w the number of weeks in a year (00-53) Monday is the start of the week%x local corresponding date for %Z the name of the current time zone to get a month calendar

The Calendar module has a wide range of methods for working with calendars and monthly calendars, such as printing a month's calendar:

#!/usr/bin/python
#-*-coding:utf-8-*-

Import calendar

cal = Calendar.month (2016, 1)
print " The following output January 2016 calendar: "
print cal;"

The above example outputs the result:

The following output January 2016 calendar:
    January 2016
Mo Tu We Th Fr Sa Su
             1  2  3
 4  5  6 7 8  9 for
25 26 27 28 29 30 31 in all of a.

Time Module

The time module contains the following built-in functions, which are both temporal and transition-time formats:

<
Serial Number function and Description
1 Time.altzone
Returns the number of offset seconds from the daylight saving time area in West Greenwich. If the region returns negative values in eastern Greenwich (e.g. Western Europe, including the United Kingdom). For daylight saving time enabled areas to be used.
2 Time.asctime ([Tupletime])
Accepts a time tuple and returns a 24-character string that is readable in the form of "Tue Dec 11 18:07:14 2008" (December 11, 2008 Tuesday 18:07 14 seconds).
3 Time.clock ()
The number of seconds to use for floating-point count returns the current CPU time. Used to measure the time consuming of different programs, more useful than time.time ().
4 Time.ctime ([secs])
The effect is equivalent to Asctime (localtime (secs)), without giving the parameter the equivalent of Asctime ()
5 Time.gmtime ([secs])
Receives a time suffix (the number of floating-point seconds after the 1970 era) and returns the time tuple T under Greenwich Astronomical time. Note: T.TM_ISDST is always 0
6 Time.localtime ([secs])
Receive a time suffix (the number of floating-point seconds after the 1970 era) and return the time tuple T (t.tm_isdst in local time) (0 or 1, depending on whether local daylight time).
7 Time.mktime (Tupletime)
Accepts a time tuple and returns a time suffix (the number of floating-point seconds after the 1970 era).
8 Time.sleep (secs)
Postpone the operation of the calling thread, secs the number of seconds.

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.