Python datetime module

Source: Internet
Author: User

#! /usr/bin/env python
#-*-Coding:utf-8-*-
From datetime import *
# 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 DateTime.
# Datetime.timedelta: Represents the time interval, which is the length between two points in time.
# Datetime.tzinfo: Related information about the time zone.

#1. Date represents a day that consists of a year, month, and date

t = date.today () #返回一个表示当前本地日期的date对象
Print (t)
Print (T.year,t.month,t.day) #年, month, day
T1 = T.replace (day=12) #date. Replace (year, month, day): Generates a new Date object, substituting the attributes in the original object with the specified years, months, and days of the parameter
Print (T1)
Print (T.timetuple ()) # returns the Time.struct_time object corresponding to the date
Print (T.toordinal ()) #返回日期对应的Gregorian Calendar Date
Print (T.weekday ()) #如果是星期一, returns 0 if it is Week 2, returns 1, and so on

T3 = t1-t #日期进行加减运算
Print (T3)
Print (T<T1) #日期比较

#2. Time, which is made up of hours, minutes, seconds, and microseconds

TM = Time (15,53,5) #构造一个time
Print (tm)

TM1 = Tm.replace (hour=13) #根据参数替换时, minutes, seconds to generate a new time
Print (TM1)

Print (Tm.hour,tm.minute,tm.second) # time, minutes, seconds

#3. DateTime class All information for date and time

td = Datetime.today () #当前本地时间的datetime对象
Print (TD)

Tu = Datetime.utcnow () #当前utc时间的datetime对象
Print (TU)

to = Datetime.combine (T,TM) #根据date和time, create a DateTime object
Print (To)

Python datetime module

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.