Python Date Action Learning notes _python

Source: Internet
Author: User
Tags datetime month name string format timedelta
Like using print ', '. Join (Datelist)
You can put all the items in a datelist list into a string, which, of course, inserts a comma between each item, which is simpler than looping.
The operation of the date 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 of the string format into a Date object
The escape character corresponds to the following meaning
%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
Day of%d months (0-31)
%H 24-hour hours (0-23)
%I 12 Hours of 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 number of weeks in a year (00-53) Sunday is the beginning of the week
%w Week (0-6), Sunday for the beginning of the week
%w number of weeks in a 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
%%% per se
============================================
#-*-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 back 10 days.
# if it's an hour, change to hours
# its commonly used classes are: datetime and Timedelta two. They can add and subtract from each other.
# Each class has a number of methods and properties to view specific values, such as datetime can view: Days, hours (hour), Weeks (Weekday ()), etc.;
# Timedelta can view: days, seconds (seconds) and so on.
#
# Time, datetime, String type convert to 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.