Python datetime and Calendar module common features

Source: Internet
Author: User
Tags timedelta

One, datetime module

The DateTime module defines the following classes:
Datetime.date: A class that represents a date. The commonly used attributes are year, month and day;
Datetime.time: A class that represents time. The commonly used properties are hour, minute, second, microsecond;
Datetime.datetime: Represents the date and time.
Datetime.timedelta: Represents the time interval, which is the length between two points in time.
Datetime.tzinfo: Related information about the time zone.



1,Datetime.datetime object

Show the current time and custom format

In [ten]: Str_time = Datetime.datetime.now ()

Or

In [ten]: Str_time = Datetime.datetime.today ()


In [all]: print Str_time
2014-11-17 20:02:10.112139

Display format customization:

in [+]: Print Str_time.strftime ("%y-%m-%d%h:%m")
2014-11-17 20:02



Convert a time-formatted string to a DateTime object

in [+]: a= "2014 11-17 20:02"
in [[]: Print Datetime.datetime.strptime (A, "%Y%m-%d%h:%m")
2014-11-17 20:02:00



Displays the current UTC time

in [+]: Print Datetime.datetime.utcnow ()
2014-11-17 12:26:59.612081



Convert (Unix) timestamps to datetime objects:

in [+]: A=time.time ()
in [+]: Print a
1416227538.62

in [+]: Print Datetime.datetime.fromtimestamp (a)
2014-11-17 20:32:18.619621

In []: Print Datetime.datetime.utcfromtimestamp (a)
2014-11-17 12:32:18.619621



2, Datetime.date Object

Show Current Date:

In [str_date]: = Datetime.date.today ()
In [all]: print str_date
2014-11-17



Displays the date object's month Day:

In [the]: print Str_date.year,str_date.month,str_date.day
2014 11 17



Convert (Unix) timestamps to date objects:

In []: A=time.time ()
in [+]: Print a
1416228263.01
In [98]: Print Datetime.date.fromtimestamp (a)
2014-11-17



3,datetime.time Object

The time class represents times, consisting of hours, minutes, seconds, and microseconds.

In [109]: TM = Datetime.time (23, 46, 10)
in [+]: Print Tm.hour,tm.minute,tm.second
23 46 10
In [113]: TM2 = Tm.replace (minute=01)
In [all]: print Tm2.hour,tm2.minute,tm2.second
23 1 10



4,datetime.timedelta Object

Show yesterday's date (can also be a few minutes or seconds or hours of time)

In [131]: A = Datetime.datetime.now ()
In [PPI]: print a
2014-11-17 20:57:17.170393
In [133]: Print a + Datetime.timedelta (days=-1)
2014-11-16 20:57:17.170393

In [137]: Print A + Datetime.timedelta (Days=1)
2014-11-18 20:57:17.170393

In [134]: Print a + Datetime.timedelta (hours=-1)
2014-11-17 19:57:17.170393
In [135]: Print a + Datetime.timedelta (minutes=-1)
2014-11-17 20:56:17.170393
In [136]: Print A + Datetime.timedelta (seconds=-1)
2014-11-17 20:57:16.170393



5, two Datetime.datetime object time difference calculation

Calculate the difference between seconds or days

in [+]: a = Datetime.datetime.now ()
In [181]: B = A + Datetime.timedelta (days=-2)
In [182]: c = A-B
In [183]: Print c.total_seconds ()
172800.0
In [184]: Print C.days
2



For detailed documentation, please see:

Https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior



Two, calendar module

Show a month for a few days

In [Calendar.monthrange]: Print (2014, 11)
(5, 30)

(5, 30) Explanation: 5 indicates that the first day of November 2014 is Saturday; 30 means November 2014 for a total of 30 days



Judging whether it is the year of the run

In [194]: Print Calendar.isleap (2012)
True
In [195]: Print Calendar.isleap (2014)
False



Determine the number of year between two years

In [196]: Print Calendar.leapdays (2000, 2014)
4



Display the calendar for a month:

In [206]: cal = Calendar.month (2011, 11)
In [207]: Print cal
November 2011
Mo Tu We Th Fr Sa Su
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30



For detailed documentation, please see:

Https://docs.python.org/2/library/calendar.html


This article is from the "Good" blog, please be sure to keep this source http://leejia.blog.51cto.com/4356849/1577662

Python datetime and Calendar module common features

Related Article

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.