Python, datetime notes.

time just ended today, build on the progress, but also get rid of the datetime forget. General debugging computer to read, and now back.

Studies preceding date is feeling more process-oriented, with which many functions.

It is more like a datetime object-oriented operations.

Online search for a point of information, point of view some of the books, ready to pick some of their own priorities considered forget to talk about.

Let me talk about datetime.datetime Well, I have been before would use datetime.datetime.now ()

When running a d1 = datetime.datetime.now () or d1 = datetime.datetime. (2019,1,1)

In fact, nature is in complete instantiation, therefore, the object method will d1 has a lot of definitions, attributes.

Because it is the operation of the example, but also some subtraction operations between different instances, the object returns datetime.timedelta.

In [244]: d1 = datetime.datetime(1985,11,5)                                                  

In [245]: d2 = datetime.datetime.now()                                                       

In [246]: isinstance(d1,datetime.datetime)                                                   
Out[246]: True

In [247]: d1-d2                                                                              
Out[247]: datetime.timedelta(days=-12425, seconds=74030, microseconds=689037)

In [248]: d1.isoweekday                                                                      
Out[248]: <function datetime.isoweekday>

In [249]: d1.isoweekday()                                                                    
Out[249]: 2

In [251]: datetime.datetime.fromtimestamp(time.time())                                       
Out[251]: datetime.datetime(2019, 11, 11, 3, 35, 10, 555170)

In [252]: datetime.datetime.now()                                                            
Out[252]: datetime.datetime(2019, 11, 11, 3, 35, 18, 959855)

datetime.datetime.fromtimestamp (time.time ()) can also be used to create a time stamp time object.

Here to do a simple code description, the object has a lot of attributes. (I feel may be more used to prepare marked red)

datetime instance method:

datetime.date ()
- returns a date object datetime.time () - Returns a time string (attribute tzinfo None)

datetime.timetz ()
- returns a time () object (with tzinfo property)

datetime. Replace ([year [, month the [, Day [, hour [, minute [, SECOND [, microsecond [, tzinfo]]]]]]]])
- generates a new date object, the specified date and time parameter instead of the original object has attribute

datetime.astimezone (TZ = None)
- a new incoming tzinfo property returns the new time zone in accordance with the adjusted datetime objects

datetime.utcoffset ()
- if tzinfo property is None, or None; otherwise self .tzinfo.utcoffset (Self)

datetime.dst ()
- If tzinfo property is None, or None; otherwise self.tzinfo.dst (Self)

datetime.tzname ()
- If tzinfo property is None, or None; otherwise Back self.tzinfo.tzname (Self)

datetime.timetuple ()
- returns the object corresponding to the date time.struct_time (similar totime module time.localtime of ())

datetime.utctimetuple ()
- Returns the UTC date corresponding time.struct_time objects

datetime.toordinal ()
- Returns the date corresponding to the date of the Gregorian Calendar (similar self.date (). toordinal ())

datetime.timestamp ()
- returns the current time timestamp (similar to the time module of time.time ())

datetime.weekday ()
- returns the day of the week from 0 to 6 represent (Monday is 0, and so on) datetime.isoweekday () - returns 1 to 7 show the week (Monday is 1, and so on) datetime.isocalendar () returns a triple format (year, week, day) # the second element is the first of several "year in Week ", the third element is the" day of the week " datetime.isoformat (on Sep = 'T') - returns a string date ISO 8601 format, such as" YYYY-MM-DD "string datetime. __ __str () - d for date object is, str (d) corresponding to d.isoformat () datetime.ctime ()














- Returns a string representation of the date, corresponding to the time module time.ctime of (time.mktime (d.timetuple ()))

datetime.strftime (the format)
- Returns a string representation of the custom formatted date, there are the following Detailed

datetime format __ .__ (format)
- You can specify data objects like datetime.strftime (format), which makes the call str.format () when the string

In fact, the process of watching, I think the most convenient thing datetime.timedelta, this can very easily make you the day before or the day after the date of entry.

In [254]: d2                                                                                 
Out[254]: datetime.datetime(2019, 11, 11, 3, 26, 9, 310963)

In [255]: t_pass = datetime.timedelta(2)                                                     

In [256]: d2+t_pass                                                                          
Out[256]: datetime.datetime(2019, 11, 13, 3, 26, 9, 310963)

In [257]: d2 -t_pass                                                                         
Out[257]: datetime.datetime(2019, 11, 9, 3, 26, 9, 310963)

 Datetime.timedelta a class, you need to specify the period of time passed to the constructor of the class. Optional parameters include: weeks, days (default), hours, minutes, seconds, microseconds

 

Finally, talk about strftime method, with which the parameters of Time. Strftime function inside the same.

 

Formatting instructions meaning
%a Short week (Monday - days: Mon, Tue, Wed, Thu, Fri, Sat, Sun)
%A The whole week writing (Monday - days: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday)
%w In the first few days of the week (0 for Sunday ... 6 for Saturday)
%d The first few days of the month (01, 02, ..., 31)
%b Shorthand month (January - December: Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec)
%B All written in January (January - December: January, February, March, April, May, June, July, August, September, October, November, December)
%m Month (01, 02, ..., 12)
%Y Year represented by two numbers (eg 2014 == 14)
%Y Year represented by four digits
%H 24-hour clock (00, 01, ..., 23)
%I 12-hour clock (01, 02, ..., 11)
%p AM or PM
%M Min (00, 01, ..., 59)
%S Sec (00, 01, ..., 59)
%f Microseconds (000000, 000001, ..., 999999)
%with And UTC time interval; if the local time, an empty string ((empty), +0000, -0400, +1030)
%WITH Time zone name; If local time, an empty string ((empty), UTC, EST, CST)
%j The first few days of the year (001, 002, ..., 366)
% U In the first few weeks of the year on Sunday as the first day (00, 01, ..., 53)
%W The first few weeks of the year on Monday as the first day (00, 01, ..., 53)
%c Represents the date and time string (Tue Aug 16 21:30:00 2014)
%x String represents the date (08/16/14)
%X String represents time (21:30:00)
%% It represents the percent sign

 

 

They can then fill in behind the object parameters as required.

 

In [258]: d2                                                                                 
Out[258]: datetime.datetime(2019, 11, 11, 3, 26, 9, 310963)

In [259]: d2.strftime('%Y-%m-%d:%H.%M.%s')                                                   
Out[259]: '2019-11-11:03.26.1573413969'

In [260]: d2.strftime('%Y-%m-%d:%H.%M.%S')                                                   
Out[260]: '2019-11-11:03.26.09'

In [261]: d2.strftime('%A %Y-%m-%d:%H.%M.%S')                                                
Out[261]: 'Monday 2019-11-11:03.26.09'

 

 

datetime module will focus on here talk about some of the behind the three reference links. There is a need to use, can refer.

 

Detailed datetime module (basic type of date and time) :

https://fishc.com.cn/forum.php?mod=viewthread&tid=51725&extra=&highlight=datetime&page=1

https://www.cnblogs.com/awakenedy/articles/9182036.html

https://blog.csdn.net/weixin_42232219/article/details/89838580

 

datetime.is
 

Guess you like

Origin www.cnblogs.com/sidianok/p/11832621.html
Recommended