Python Learning notes (18)

Source: Internet
Author: User

I. INTRODUCTION of DATETIME

DateTime is the standard library for Python to process dates and times


Second, import datetime date time processing Standard library

# datetime is a datetime module, which includes a DateTime class with the same name
From datetime import datetime


Third, get the current date and time information

# Get current datetime information
now = DateTime.Now ()


Iv. specified date and time

DT = DateTime (2015,3,4,12,12,23)


V. Convert date time to Unix time suffix

# convert datetime objects to UNIX time prefixes
Dt.timestamp ()


Vi. converting a time suffix to a date time

# Convert a time suffix to a date time
t = 1565455645
DT = Datetime.fromtimestamp (t)


Seven, string conversion to DateTime

# Convert String to Datetiem
# One of the rear is the date-time matching pattern: see Https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior for details
# Note that there is no time zone information after the conversion
Cday = Datetime.strptime ("2015-6-1 18:56:52", "%y-%m-%d%h:%m:%s")


Eight, date time converted to string

now = DateTime.Now ()
str = now.strftime ("%a,%b,%d%h:%m")


ix. addition and subtraction of datetime

# Add and subtract to date time need to import Timedalta this class
From datetime import Datetime,timedalta
now = Datetime.datetime ()
Now + Timedalta (hours=10) # on the basis of the original time, after another 10 hours between
now = Timedalta (day=2,hours=10) #时间过后两天, time after 10 hours after the hour


Ten, get local time

Import time
# Get local time
LocalTime = Time.localtime (Time.time ())
Print ("local time:", localtime)

Struct_time Time Tuple:
Tm_year
Tm_mon
Tm_mday
Tm_hour
Tm_min
Tm_sec
Tm_wday
Tm_yday
Tm_isdst


Xi. get a calendar of a month

Import Calendar

Cal = Calendar.month (year, month)
Print (CAL)

  

  


Python Learning notes (18)

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.