Python Time: DateTime module

Source: Internet
Author: User
Tags timedelta

DateTime encapsulates a number of methods on a time basis. But time is often used, the function in datetime, time can achieve one, datetime of three modules Datetime.datedatetime.timedatetime.datetime mainly use this module import Datetime1, Datetime.datetime.now () Gets the current time, DateTime format T1=datetime.datetime.now () print T1print T1.yearprint T1.day2017-05-28 11:32:21.853000 (subtle: 853000) 201728 2, fromtimestamp timestamp converted to Datetimet2=time.time () dt= Datetime.datetime.fromtimestamp (T2) Print Dtprint type (dt) Result: 2017-05-28 11:34:20.725000<type ' Datetime.datetime ' > 3, strftime () formatted output print Datetime.datetime.now (). Strftime ('%y-%m-%d%h:%m:%s ')  4, replace () Replace a value in datetime with the desired value T1=datetime.datetime.now () t2=t1.replace (month=4,hour=23) Print T2 result: 2017-04-28 23:50:10.855000t3=t1-t2print type (t3), t3 result: <type ' Datetime.timedelta ' > Days, 12:00:00 5, Datetime.timedelta solve some time difference problem class Datetime.timedelta ([Days,[,hours[,minutes[,seconds[,microseconds]]]) dt= Datetime.timedelta (days=100) Newtime=t1+dt date after 100 days print newtime result: 2017-09-05 11:54:52.479000dt1= Datetime.timedelta (days=-100) newtime=T1+DT1 100 days ago Date  6, Total_seconds () converts the time to seconds Seconds=datetime.timedelta (days=3). Total_seconds () Convert three days to seconds print Seconds results: 259200.0

Python time: DateTime module

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.