datetime and time module

Built-in modules: datetime

Import module: from datetime import datetime

! # / usr / bin / env Python 
# - * - Coding: UTF-8 - * - 
Import Time 
from Import datetime datetime, TimeZone, timedelta 

# ################## ###### datetime format acquisition time ############################## 
"" " 
v1 = DateTime.Now () # current local time (Eastern area 8) 
Print (v1) 
TZ = TimeZone (timedelta (hours = 7)) # 7 current Eastern time 
v2 = DateTime.Now (TZ) 
Print (v2) 
v3 = DateTime.UtcNow () # current UTC time 
Print (V3) 
"" " 

# ######################## string format to convert a datetime ######## ###################### 
"" " 
V1 = DateTime.Now () 
Print (V1, type (V1)) 
Val = v1.strftime (" the Y% -% m-% d% H: % m:% S ") # some windows do not have Chinese support formatting, such as"% Y% m on month "Because inconsistent with the code, but do not worry about the mac and linux in 
Print (Val) 
"" "

# ######################## string converted into datetime #################### ########## 
"" " 
V1 = datetime.strptime ( 'on 2011-11-11', 'M-%%% Y-D') 
Print (V1, type (V1)) 
" "" 

# ######################## datetime time of addition and subtraction #################### ########## 
"" " 
V1 = datetime.strptime ( 'on 2011-11-11', 'M-%%% Y-D') 
V2 = V1 - timedelta (Days = 140) 
DATE = v2.strftime ( 'M-%%% Y-D') 
Print (DATE) 
"" " 

# ######################## stamp and datetime relationship ############################## 
"" " 
ctime = time.time () 
Print (ctime) 
v1 = datetime. fromtimestamp (the ctime) 
Print (V1) 

V1 = datetime.now()
val = v1.timestamp()
print(val)
"""

  

Time Format table:

Formatter Explanation

%a

Abbreviations week English words: as Monday, Mon Returns
%A Week spelling of English words: as Monday, returned Monday
%b Abbreviated English words month: as of January, Jan returns
%B Abbreviation of the word citation month: as of January, January return
%c  Datetime Returns a string representation, such as Sun Nov 3 16:26:22 2019
%d It returns the current time is the first few days of the current month
%f Microseconds indicates: Range: [0,999999]
%H It indicates the current hour in 24-hour
%I It indicates the current hour to 12-hour
%j Returns the day is the day of the year range [001,366]
%m Returns the month range [0,12]
%M Returns the number of minutes the range [0,59]
%P Returns the morning or afternoon - AM or PM
%S Returns the seconds range [0,61]. . . The manual explains
% U Return the week was the first week of the year with Sunday as first day
%W Return the week was the first week of the year with Monday as the first day
%w Day number of days in a week, in the range [0, 6], for Saturday 6
%x The string representation of a date: 03/08/15
%X String representation: 23: 22:08
%Y Two numbers indicate the year: 2019 19
%Y Four digits indicate the year of 2015
%with And utc time interval (if the local time, an empty string)
%WITH Time zone name (if local time, returns an empty string)

A few date formatting Comprehensive examples:

#!/usr/bin/env python
# -*- coding:utf-8 -*-

from datetime import datetime

dt = datetime.now()
print('时间:(%Y-%m-%d %H:%M:%S %f): ', dt.strftime('%Y-%m-%d %H:%M:%S %f'))
print('时间:(%Y-%m-%d %H:%M:%S %p): ', dt.strftime('%y-%m-%d %I:%M:%S %p'))
print('星期缩写%%a: %s ' % dt.strftime('%a'))
print(' Week A spelling %%: S% ' % dt.strftime ( ' % A ' ))
 Print ( ' Month abbreviations B %%: S% ' % dt.strftime ( ' % B ' ))
 Print ( ' full month batch B %%: S% ' % dt.strftime ( ' % B ' ))
 Print ( ' date time C %%: S% ' % dt.strftime ( ' % C ' ))
 Print ( ' today this week % s of day ' % dt.strftime ( ' % W '))
print(' Today is the first day of this year's% s ' % dt.strftime ( ' % J ' ))
 Print ( ' this week is the first week this year is% s ' % dt.strftime ( ' % U ' ))
 Print ( ' Today is the first day of the month% s ' % dt.strftime ( ' % D ' )) 

# output results are as follows: 
"" " 
time: (% Y-% m-% d % H:% M:% S% f): 2019 -11-03 16:30:49 525 852 
time: (% Y-% m-% d % H:% M:% S% p): 19-11-03 04:30:49 PM 
weeks Abbreviation% a: Sun 
week spelling% a: Sunday 
February abbreviation% b: Nov 
January the whole batch% B: November 
date time% c: Sun Nov 3 16:30:49 2019 
today is the day 0 this week
Today is the first 307 days of
This week is the first 44 weeks of this year, 
today is the first 03 days of the month 
. "" "
View Code

 

Built-in modules: time

1. time.time () # Returns the current time timestamp (after the 1970 era through the floating-point number of seconds).

For example:

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import time
now_time = time.time()
print(now_time)

Output:

1572770580.673631

  

2. time.sleep () # make the program dormant for how long, in seconds.

Example: calculate how long to run

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import time

old_time = time.time()
time.sleep(2)
new_time = time.time()
result = new_time - old_time
print(result)

Output:

2.0001912117004395

  

3. time.localtime () # results returned tuples to be named (refer to collections in namedtuple Method)

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import time

p = time.localtime()
print(p)

Output:

time.struct_time(tm_year=2019, tm_mon=11, tm_mday=3, tm_hour=16, tm_min=49, tm_sec=58, tm_wday=6, tm_yday=307, tm_isdst=0)

  

Guess you like

Origin www.cnblogs.com/L521Z/p/11787959.html