#Generate a fixed format time representation format
time.asctime (time.localtime ())
time.ctime (time.time ())
# Wed Oct 26 16:45:08 2016
Attribute value
tm_year (year) such as 2011
tm_mon (month) 1-12
tm_mday (Sun) 1-31
tm_hour (hours) 0-23
tm_min (minutes) 0-59
tm_sec (seconds) 0-61
tm_wday (weekday) 0-6 (0 means Sunday)
tm_yday (day of the year) 1-366
tm_isdst (whether it is daylight saving time) The default is -1
format time structured representation
Format Meaning
% a locale simplified week name
% A local full week name
% b local simplified month name
% B local full month name
% c local corresponding date and time representation
% d day of the month (01-31)
% H Hour of day (24-hour clock, 00-23)
% I first hour (12-hour clock, 01-12)
% j day of the year (001-366)
% m month (01-12)
% M minutes (00-59)
% p corresponding to local am or pm
% S seconds (01-61)
% U Week of the year. (00-53 Sunday is the beginning of a week.) All days before the first Sunday are placed in week 0.
% w day of the week (0-6, 0 is Sunday)
% W and% U are basically the same, except that% W starts with Monday as a week.
% x local corresponding date
% X local corresponding time
% y year with century removed (00-99)
% Y full year
% Z name of the time zone (null if not present)
%% ‘%’ character
Common structured time combinations:
print time.strftime ("% Y-% m-% d% X")
# 2016-10-26 20:50:13
3, time addition and subtraction
#timestamp Addition and Subtraction Units in Seconds
import time
t1 = time.time ()
t2 = t1 + 10
print time.ctime (t1) #Wed Oct 26 21:15:30 2016
print time.ctime (t2) #Wed Oct 26 21:15:40 2016
Second, the datetime module
The datatime module repackages the time module and provides more interfaces. The classes provided are: date, time, datetime, timedelta, tzinfo.
1.date class
datetime.date (year, month, day)
Static methods and fields
date.max, date.min: the maximum and minimum dates that a date object can represent;
date.resolution: The smallest unit of a date object representing a date. Here is the day.
date.today (): returns a date object representing the current local date;
date.fromtimestamp (timestamp): returns a date object according to the given time;
d1 = date (2011,06,03) #date object
d1.year, date.month, date.day: year, month, day;
d1.replace (year, month, day): generate a new date object, replacing the attributes in the original object with the year, month, and day specified by the parameters. (The original object remains unchanged)
d1.timetuple (): returns the time.struct_time object corresponding to the date;
d1.weekday (): returns weekday, if it is Monday, it returns 0; if it is week 2, it returns 1, and so on;
d1.isoweekday (): returns weekday, if it is Monday, returns 1; if it is week 2, returns 2, and so on;
d1.isocalendar (): returns a tuple in the format (year, month, day);
d1.isoformat (): Returns a string with a format such as 'YYYY-MM-DD';
d1.strftime (fmt): Same as the time module format.
datetime.time (hour [, minute [, second [, microsecond [, tzinfo]]]])
Static methods and fields
time.min, time.max: the minimum and maximum time that the time class can represent. Where time.min = time (0, 0, 0, 0) and time.max = time (23, 59, 59, 999999);
time.resolution: the smallest unit of time, here is 1 microsecond;
Methods and properties
t1 = datetime.time (10,23,15) #time object
t1.hour, t1.minute, t1.second, t1.microsecond: hours, minutes, seconds, microseconds;
t1.tzinfo: time zone information;
t1.replace ([hour [, minute [, second [, microsecond [, tzinfo]]]]]): Create a new time object, replacing the original object with the hour, minute, second, and microsecond specified by the parameters Properties (the original object remains unchanged);
t1.isoformat (): Returns a string representation in the format "HH: MM: SS";
t1.strftime (fmt): same as the format in the time module;
datetime is equivalent to the combination of date and time.
datetime.datetime (year, month, day [, hour [, minute [, second [, microsecond [, tzinfo]]]]])
Static methods and fields
datetime.today (): returns a datetime object representing the current local time;
datetime.now ([tz]): returns a datetime object representing the current local time. If the parameter tz is provided, the local time in the time zone indicated by the tz parameter is obtained
datetime.utcnow (): Returns a datetime object for the current UTC time; #Greenwich Mean Time
datetime.fromtimestamp (timestamp [, tz]): create a datetime object according to the time, the parameter tz specifies the time zone information;
datetime.utcfromtimestamp (timestamp): create a datetime object based on the time;
datetime.combine (date, time): create a datetime object based on date and time;
datetime.strptime (date_string, format): convert the format string into a datetime object;
dt = datetime.now () # datetime object
dt.year, month, day, hour, minute, second, microsecond, tzinfo:
dt.date (): Get the date object;
dt.time (): Get the time object;
dt. replace ([year [, month [, day [, hour [, minute [, second [, microsecond [, tzinfo]]]]]]]]):
dt. timetuple ()
dt. utctimetuple ()
dt. toordinal ()
dt. weekday ()
dt. isocalendar ()
dt. isoformat ([sep])
dt. ctime (): returns a date format C string, equivalent to time.ctime (time.mktime (dt.timetuple ()))
dt. strftime (format)
4.timedelta class, time addition and subtraction
Using timedelta, you can easily calculate days, hours, minutes, seconds, milliseconds, and delicate time on the date. If you want to calculate the month, you need another method.
#coding: utf-8
from datetime import *
dt = datetime.now ()
#Date minus one day
dt1 = dt + timedelta (days = -1) # yesterday
dt2 = dt-timedelta (days = 1) # yesterday
dt3 = dt + timedelta (days = 1) # tomorrow
delta_obj = dt3-dt
print type (delta_obj), delta_obj # <type ‘datetime.timedelta‘> 1 day, 0:00:00
print delta_obj.days, delta_obj.total_seconds () # 1 86400.0
5, tzinfo time zone class
#! / usr / bin / python
# coding = utf-8
from datetime import datetime, tzinfo, timedelta
"" "
tzinfo is a class about time zone information
tzinfo is an abstract class, so it cannot be instantiated directly
"" "
class UTC (tzinfo):
"" "UTC" ""
def __init __ (self, offset = 0):
self._offset = offset
#Beijing time
beijing = datetime (2011,11,11,0,0,0, tzinfo = UTC (8))
print "beijing time:", beijing
#Bangkok Time
bangkok = datetime (2011,11,11,0,0,0, tzinfo = UTC (7))
print "bangkok time", bangkok
#Beijing Time to Bangkok Time
print "beijing-time to bangkok-time:", beijing.astimezone (UTC (7))
#The time zone is also considered when calculating the time difference
timespan = beijing-bangkok
print "Time difference:", timespan
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.