Timedatetime module of common python modulesIn the usual code, we often have to deal with time. In Python, the modules related to time processing include: times and DateTime, as described below:Before you begin, there are several ways to represent the time:
1. Time stamp
2. Formatted time string (Time object)
3. Tuples (struct_time) a tot
When writing code, you will always encounter a variety of time processing and requirements. To summarize:First of all, this article is a summary of the DateTime module.Begin.First, DateTime basic operation1. Get the current datetime1 times = datetime.datetime.now ()2 print time3# Output The following date-the hour (accurate to microseconds, 6 digits after decimal point)45 2018-01-11 20:19:34.7940002. Get Da
nested list of integers. Each sub-list is loaded with integers representing one weeks. The date of year month is set to 0, and the day of the month is indicated by the day of the week, starting from 1.Calendar.monthrange (Year,month)Returns a two integer. The first is the day of the week of the month, and the second is the date code for that month. Day from 0 (Monday) to 6 (Sunday); The month is from 1 to 12.Calendar.prcal (year,w=2,l=1,c=6)Equivalent to print Calendar.calendar (year,w,l,c).Cal
Use the DateTime module to get the current date and time1 Import datetime 2 i=datetime.datetime.now ()3print (" current date and time is%s"% IUse the DateTime module to get the current year1 Import datetime 2 i=datetime.datetime.now ()3print (" Current year is%s" % I.year)Use the D
character typePython code
in [+]: Time.strftime ("%y%m%d", Time.localtime ())
out[]: ' 20130810 '
Strptime (string, format) converts a time string to an array of time based on the specified format characterPython code
in [+]: time.strptime (' 20130810 ', "%y%m%d")
out[]: Time.struct_time (tm_year=, tm_mon=8, tm_mday=, tm_hour=0, tm_min=0, tm_sec= 0, tm_wday=5, tm_yday=222, tm_isdst=-1)
Two. DateTime mo
, A, 790945) in []: D1.strftime ('%y-%m-%d%h:%m:%s ') out[28]: ' 2014-08-15 08:12:34 ' 4.datetime.datetime.timple () into the struct_time format, which is passed to Time.mktime (t) and then directly into timestamp format in []: d1out[29]: Datetime.datetime (8, 8, 790945) in [+]: D1.timetuple () out[30]: Time.struct_time (tm_year=2014, tm_mon=8 , tm_mday=15, Tm_hour=8, tm_min=12, tm_sec=34, tm_wday=4, tm_yday=227, Tm_isdst=-1) in [to]: Time.mktime (D1.timetuple ()) out[31]: 1408061554.05.datetime
Description: DateTime is a combination of date and time, containing all the information of dateCommon Uses for datetime:1, Datetime.datetime.now () returns a DateTime object of UTC time>>> from datetime import *>>> Print DateTime.Now ()2017-09-05 16:36:59.6370002, Datetime.datedelta () and Datetime.datetime.now () comb
/%m/%d%h:%m:%s ', timetuple) Time-date formatting symbols in Python: %y Two-digit year (00-99) %y Four-digit year represents (000-9999)%m month (01-12) the day of the Month (0-31)%H 24-hour hours (0-23) %I 12-hour hours (01-12) %M minutes (00=59) %s seconds (00-59) %a Local Simplified week name%a local full week name %b locally Simplified month name %b Local full month name %c Local corresponding date representation and time represents
Import datetime, CalendarDate = Datetime.datetime.now () Gets the current system time#1, return to yesterday's dateDef getyesterday ():Today=datetime.date.today ()Oneday=datetime.timedelta (Days=1)Yesterday=today-onedayreturn yesterday#2, return to today's dateDef gettoday ():Return Datetime.date.today ()#3, gets the date of the previous day of the given argument, returns a listdef getdaysbynum (num):Today=datetime.date.today ()Oneday=datetime.timedel
#! /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 th
Time ModuleTmie.strptime: Converting a time string to a time typeFormat: Time.strptime (string[string[, format])Results can be used to return the year using Time.tm_year and the remainder to be identical.# Example>>> t = time.strptime ("2018-2-2", "%y-%m-%d")>>> TTime.struct_time (tm_year=2018, tm_mon=2, tm_mday=2, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=4, tm_yday=33, Tm_isdst=-1)>>> T.tm_year2018Time.strftime: Converts a Time type to a string of the
This article briefly summarizes the Python processing time and date aspects of the module, mainly the use of datetime, three modules, I hope this article is useful for learning Python friends.
The first is the call of the module, many Ides have installed a lot of Python frequently used modules, so we do not need to in
Python projects often need to convert time between datetime format and timestamp format, or you need to convert UTC time to local time, this article summarizes the transformation between these time functions, for your reference.
One, DateTime converted to timestamp
def datetime2timestamp (DT, convert_to_utc=false):
' Converts a
########################## # Python Time Conversion # ##########################
--------------------------A. The type of time representation--------------------------
The time that may be involved in PY is four types of 1. Time String 2. DateTime tuple (DateTime obj) 3. Time tuple (Time obj) 4. Timestamp
1. Time StringString is the simplest way to represent ti
22:03:04.6869232018-01-17 22:03:042018-01-17 22:03:04.6874862018-01-17 21:03:04.687486Iii. using datetime to get the length of code execution# Get code execution time with timestampsS_time = Time.time ()For I in Range (0,10): Time.sleep (1)E_time=time.time ()Print (' Code run Time: ', e_time-s_time)Execution Result:Code run time is: 10.003105163574219Four, time stamp and string conversion of each other# string formatting time conversion timestampS
1. Date output formatted datetime/= stringImport datetimenow = Datetime.datetime.now ()Now.strftime ('%y-%m-%d%h:%m:%s ')Output' 2015-04-07 19:11:21 'Strftime is an instance method of a DateTime class.2. Date Output Formatted string = DateTimeImport datetimeT_str = ' 2015-04-07 19:11:21 'D = datetime.datetime.strptime (T_str, '%y-%m-%d%h:%m:%s ')Strptime is a static method of a
formatDatetime.datetime.now (). Strftime ('%b-%d-%y%h:%m:%s ')' apr-16-2017 21:01:35 'Datetime.datetime.strptime (): converted from string format to date formatDatetime.datetime.strptime (' apr-16-2017 21:01:35 ', '%b-%d-%y%h:%m:%s ')2017-04-16 21:01:35Iv. Timedelta class for DateTimeThe Datetime.datetime.timedelta is used to calculate the difference between two dates, for example:>>> A=datetime.datetime.now ()>>> B=datetime.datetime.now ()>>> aDatet
First, prefaceLearn about Python processing time-related modules, times, datetimeSecond, Time moduleFirst look at the time moduleTake a look at help (time) to see what functions are available under the time module: time() -- return current time in seconds since the Epoch as a float clock() -- return CPU time since process start as a float sleep() -- delay for a number of seconds given as a float gmtime() -- convert seconds since Epoch to U
This article is reproduced from: http://www.cnblogs.com/zhangbo127/p/4548898.html
Two ways to get the current time
Import Datetime,time Now
= Time.strftime ("%y-%m-%d%h:%m:%s")
print now
= Datetime.datetime.now ()
Print Now
Get one hour, day before, previous week, first one months before current time
Import datetime Now
= Datetime.datetime.now ()
# One hour before
D1 = Now-datetime.timedelta (hours=1
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.