Python date operations learning notes

Source: Internet
Author: User
Tags month name timedelta

For example, print ','. Join (datelist)
You can combine all the items in the datelist list into a string. Of course, this expression inserts a comma in the middle of each project. This method is more concise than the loop method.
Date operations must use the time or datetime Library
Import time
>>> S = "2006-1-2"
>>> Time. strptime (S, "% Y-% m-% d)
This is to convert the date and time in string format into a date object.
The meanings of escape characters are as follows:
% A local simplified week name
% A local full week name
% B local simplified month name
% B local full month name
% C local Date and Time
One day in % d month (0-31)
% H hour in 24-hour format (0-23)
% I 12-hour (01-12)
% J one day in the year (001-366)
% M month (01-12)
% M minutes (00 = 59)
% P local equivalent of a. m. or P. M.
% S seconds (00-59)
% U number of weeks in a year (00-53) Sunday is the start of the week
% W Week (0-6), Sunday is the beginning of the week
% W number of weeks in a year (00-53) Monday is the start of the week
% X local date Representation
% X Local Time Representation
% Y two-digit year (00-99)
% Y indicates the four-digit year (000-9999)
% Z Current Time Zone name
% Itself
========================================================== ====
#-*-Coding: UTF-8 -*-
Import time
Import datetime
#2007-11-25 15:36:35
# Using the datetime module can easily solve this problem, for example:
D1 = datetime. datetime (2005, 2, 16)
D2 = datetime. datetime (2004, 12, 31)
# Result: 47
Print (D1-D2). Days
# The preceding example shows how to calculate the number of days for two different dates.
Starttime = datetime. datetime. Now ()
Endtime = datetime. datetime. Now ()
Print (endtime-starttime). Seconds
# The previous example demonstrates the example of calculating the running time, which is displayed in seconds.
D1 = datetime. datetime. Now ()
D3 = D1 + datetime. timedelta (days = 10)
Print STR (D3)
Print d3.ctime ()
# The previous example shows the 10 days after calculating the current time.
# Replace hour days with hours
# The common classes in this document are datetime and timedelta. They can be added or subtracted from each other.
# Each class has some methods and attributes to view specific values, such as datetime: day, hour, and weekday;
# Timedelta can be viewed in days and seconds.
#
# Time, datetime, and string Conversions
#
# String-> time
# Time. strptime (publishdate, "% Y-% m-% d % H: % m: % s ")
#
# Time-> string
# Time. strftime ("% Y-% m-% d", T)
Date = '2014-01-01'
Print type (date)
Date = time. strptime (date, "% Y-% m-% d ")
Print type (date)
Print date [0]
D4 = datetime. datetime (date [0], date [1], date [2])
Print D4
Print type (D4)
# Convert a date and time object to a string.
Date = time. strftime ("% Y-% m-% d", date)
Print type (date)
# Where D is a date and time object
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.