Time and datetime for Python

Source: Internet
Author: User

#python format symbols in time date%y Two-digit year representation (00-99)%Y Four-digit year representation (000-9999)%m Month (01-12)One day in%d months (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 Local simplified month name%B Local Full month name%c Local corresponding date representation and time representation%j Day of the Year (001-366)%p equivalent of local a.m. or P.M.%u days of the year (00-53) Sunday for the beginning of the week%w Week (0-6), Sunday for the beginning of the week%W number of weeks in the year (00-53) Monday for the beginning of the week%x Local corresponding date representation%X Local corresponding time representation%Z name of the current time zonePercent% of the number itself

 int tm_sec;          /* seconds – The value interval is [0,59] */ int tm_min;             /*-The range of values is [0,59] */ int tm_hour;         /* Time-value interval is [0,23] */ int tm_mday;      /* Date in one months-the value interval is [1,31] */ int tm_mon; /* Month (starting from January, 0 for January)-<span style= color : #cc0000;               " > value interval is [0,11]</span> */ int tm_year;             /* Year with a value starting from 1900 */ int tm_wday;             /* Week – The value interval is [0,6], where 0 represents Sunday, 1 for Monday, and so on */ int tm_yday;            /* days from January 1 of each year – the value interval is [0,365], where 0 represents January 1, 1 for January 2, and so on */ int tm_isdst; The /* Daylight saving time identifier, TM_ISDST is positive when daylight savings is applied. Without daylight saving time, TM_ISDST is 0; When you are not aware of the situation, 

In a computer, time is actually represented by a number. We call the time of the January 1, 1970 00:00:00 utc+00:00 time zone The epoch time, which is recorded as 0 (the time before 1970 is timestamp negative), and the current is the number of seconds relative to the epoch. Called timestamp.

1 Import Time2 3>>>Print(Time.time ())41509073491.4012895 6 #time.localtime (secs)7>>>Print('localtime', Time.localtime (1509066702.2862496))#accepts the specified timestamp and returns the corresponding local time-date tuple8LocalTime time.struct_time (tm_year=2017, tm_mon=10, tm_mday=27, tm_hour=9, tm_min=11, tm_sec=42, tm_wday=4, tm_yday= Tm_isdst=,0)9>>>Print('localtime', Time.localtime ())#returns a tuple of the local time date corresponding to the current timeTenLocalTime time.struct_time (tm_year=2017, tm_mon=10, tm_mday=27, tm_hour=11, tm_min=4, tm_sec=51, tm_wday=4, tm_yday= Tm_isdst=,0) One  A #time.mktime (tupletime) -t = (2017, 10, 27, 9, 3, 38, 1, 48, 0)#The time tuple must have nine parameters Tupletime (YEAR,MON,DAY,HOUR,MIN,SEC,WDAY,YDAY,ISDST) -secs = Time.mktime (t)#accepts a time tuple and returns a timestamp object the>>>Print('Timestamp:%f'%secs) -timestamp:1509066218.000000 -  -  + #Time.gmtime (timestamp) accepts a timestamp and returns a time tuple ->>>Print('gmtime:', Time.gmtime (1509066218.000000))#Specify the time stamp corresponding to the GMT tuple +Gmtime:time.struct_time (tm_year=2017, tm_mon=10, tm_mday=27, Tm_hour=1, tm_min=3, tm_sec=38, tm_wday=4, tm_yday=300, tm_isdst=0) A>>>Print('gmtime:', Time.gmtime (Time.time ()))#GMT tuple corresponding to the current timestamp atGmtime:time.struct_time (tm_year=2017, tm_mon=10, tm_mday=27, tm_hour=3, tm_min=4, tm_sec=51, tm_wday=4, tm_yday=300, tm_isdst=0) -  - #Time.asctime (Tupletime) accepts a time tuple, returning the format of the time after formatting -secs= 1509066218.000000 ->>>Print("asctime (localtime (secs)):%s"% Time.asctime (time.localtime (secs)))#Local format for local specified timestamp -Asctime (localtime (secs)): Fri Oct 27 09:03:38 2017 in  ->>>Print('asctime (gmtime (secs)):%s'% Time.asctime (time.gmtime (secs)))#returns the format of Greenwich time for a specified timestamp toAsctime (gmtime (secs)): Fri Oct 27 01:03:38 2017 +  ->>>Print('asctime:', Time.asctime ())#no parameter is the formatting of the current time theAsctime:fri OCT 27 11:04:51 2017 *  $ Panax Notoginseng #time.strptime (str,fmt= '%a%b%d%h:%m:%s%Y ') The format parameter specifies the time form ->>>Print('Tupletime:', Time.strptime ("Oct-Ten",'%d%b%y%H'))#resolves a given date to a time tuple the  + #Time.strftime (format,tupletime) accepts format format and a time tuple, and returns the formatting as a custom-defined A>>>Print('Now_time:', Time.strftime ("%y-%m-%d%h:%m:%s", Time.localtime ())) theNow_time:2017-10-27 11:04:51 +  - #time.ctime (secs) If a timestamp argument is given, returns the format after the specified timestamp is formatted. $>>>Print('time.ctime:%s'% Time.ctime (secs=1509066218.000000)) $Time.ctime:Fri OCT 27 09:03:38 2017 ->>>Print('time.ctime:%s'%time.ctime ()) -Time.ctime:Fri OCT 27 11:04:51 2017 the  - #time.sleep (secs) #当前线程推迟s秒执行WuyiTime.sleep (2)

DateTime module

Two methods:

1.import datetime

2.form datetime Import DateTime

The previous datetime represents a module, which is a class of a DateTime module, so datetime two methods

I used the second kind of more concise.

#The now () function is used to get the current time>>>Print('Nowtime:', DateTime.Now ()) Nowtime:2017-10-27 11:34:49.443648#You can also export the dates you specify in the format>>>Print('MyTime:', DateTime (1997, 11, 3, 13, 13, 13)) MyTime:1997-11-03 13:13:13#converts the specified time to a timestamp>>>Print('Mystamp:', DateTime (1997, one, 3, in.). Timestamp ())#the timestamp () function is called heremystamp:878533993.0#Convert a timestamp to a time>>>Print('MyTime:', Datetime.fromtimestamp (878533993.0)) MyTime:1997-11-03 13:13:13#strptime (date_string, format) is used to format the string, and the time module uses the same#strftime (self,format) output format subject to customization>>>Print(DateTime.Now (). Strftime ('%y-%m-%d'))2017-10-27

Time and datetime for Python

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.