Python Date Action Learning notes

Source: Internet
Author: User
Tags month name timedelta
For example, with print ', '. Join (Datelist)
You can put all the items in the Datelist list into a string, which, of course, inserts a comma in the middle of each item, which is more concise than looping.
Date operation must use the time or datetime library
Import time
>>> s= "2006-1-2"
>>> Time.strptime (S, "%y-%m-%d")
This is the date and time that the string format is converted into a Date object
The corresponding meaning of the escape character is as follows
%a Local Simplified Week name
%A Local Full week name
%b a locally simplified month name
%B Local Full month name
%c Local corresponding date representation and time representation
One day in%d months (0-31)
%H 24-hour hours (0-23)
%I 12-hour hours (01-12)
%j Day of the Year (001-366)
%m Month (01-12)
%M minutes (00=59)
%p the equivalent of a local a.m. or p.m.
%s seconds (00-59)
%u weeks of the year (00-53) Sunday is the beginning of the week
%w Week (0-6), Sunday for the beginning of the week
%W Week of the Year (00-53) Monday is the beginning of the week
%x Local corresponding date representation
%x Local corresponding time representation
%y Two-digit year representation (00-99)
%Y Four-digit year representation (000-9999)
%Z the name of the current time zone
Percent% of the number itself
============================================
#-*-Coding:utf-8-*-
Import time
Import datetime
# 2007-11-25 15:36:35
#使用datetime模块可以很方便的解决这个问题, examples are as follows:
D1 = Datetime.datetime (2005, 2, 16)
D2 = Datetime.datetime (2004, 12, 31)
# Results: 47
Print (D1-D2). Days
#上例演示了计算两个日期相差天数的计算.
StartTime = Datetime.datetime.now ()
Endtime = Datetime.datetime.now ()
Print (endtime-starttime). seconds
#上例演示了计算运行时间的例子, displayed in seconds.
D1 = Datetime.datetime.now ()
D3 = D1 + Datetime.timedelta (days =10)
Print STR (d3)
Print D3.ctime ()
# The previous example shows the time to calculate the current time backwards by 10 days.
# if it's the hour days change to hours
# The classes used in this book are: DateTime and Timedelta two. They can be added and reduced between each other.
# Each class has some methods and properties to view specific values, such as DateTime can be viewed: Days (day), hours (hour), Day of the Week (weekday ()), etc.;
# Timedelta can view: days, seconds (seconds), etc.
#
# Time, datetime, string types convert each other
#
# string-Time
# Time.strptime (publishdate, "%y-%m-%d%h:%m:%s")
#
# Time-string
# time.strftime ("%y-%m-%d", T)
Date = ' 2007-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)
#将日期时间对象转成字符串则要用
Date = Time.strftime ("%y-%m-%d", date)
Print type (date)
#其中d为日期时间对象
  • 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.