程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Python training camp] Python daily practice - day 20: Monday (datetime module application)

編輯:Python


Hello, Hello, my name is Dream ah , An interesting Python Blogger , Please go easy on me
2021 Blog star of the year TOP100,2021 Blog star of the year TOP5,Python Quality creators in the field , Welcome to cooperate with me ( At the end of the article VX Want to join the learning exchange group or Learning materials welcome +++)
Introductory notes : This paradise never lacks genius , Hard work is your final ticket !
Last , May we all shine where we can't see , Let's make progress together
“ Ten thousand times sad , There will still be Dream, I've been waiting for you in the warmest place ”, It's me ! Ha ha ha ~

Preface :【Python Training camp 】 Is aimed at Python A war created by language learning Brush questions and revel party! If you don't grasp the solid foundation of knowledge , Welcome to this course :Python Public class It's best to use it together ~ If you like, hurry up and subscribe ! If you don't have self-control or motivation to learn and communicate together , Welcome to send a private letter or add my... At the end of the text VX, I will pull you into the learning exchange group , Let's exchange and study together , Group punch in

Python Practice every day :

  • Title Description
  • Their thinking
  • The source code to share
  • Learning summary
  • The articles ---- Good article recommends

Title Description

Title Description
This question is to fill in the blanks , Just calculate the result , Use the output statement in the code to output the filled results .

Whole 2020 century (19011901 year 11 month 11 solstice 20002000 year 1212 month 3131 Between days ), How many Mondays are there in all ?( Don't tell me you don't know what day it is today )

 Operation limit
Maximum operation time :1s
Maximum running memory : 128M

Their thinking

  • datetime Module date Function to get the date , Get the first and last dates respectively :start = date(1901, 1, 1) -end = date(2001, 1, 1)
  • timedelta(1) function , Medium 1 For variables , The meaning of tomorrow , After each cycle , Give Way start The date of becomes the next day :start += temp

The source code to share

# Time : 2022/2/13 22:56
# File : Monday .py
# Author : yes Dream ah !
# VX : Xu18300396393
# Ten thousand times sad , There will still be Dream, I've been waiting for you in the warmest place !
from datetime import *
start = date(1901, 1, 1) # 1901-01-01
# print(start)
end = date(2001, 1, 1)
temp = timedelta(days=1)
n = 0
while start != end:
if start.weekday() == 0:
n += 1
start += temp
print(n)

Learning summary

datetime Some uses of the module are as follows :

from datetime import datetime
from datetime import timedelta
import time
# 1) Get the current date and time 
now_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") # At this time, the time is month, day, hour, minute and second 
now_time = time.strftime("%Y-%m-%d %H:%M:%S") # At this time, the time is 
today = datetime.today() # When returning the current time, every minute and second is 0
print(" current time ")
print(today)
today1 = datetime.now() # Returns the current date and time 
now.hour # when 
now.minute # branch 
now.isoweekday()# Back to 1-7 On behalf of the Monday -- Sunday ;
now.weekday()# Back to 0-6 On behalf of the Monday -- By Sunday 
# The standard format is %w 1-6 It means Monday -- Saturday ,0 On behalf of the Sunday 
print( today1 )
today2 = datetime.utcnow() # The time to return to the current Dongba district is less than that at that time 8 Hours 
print(today2)
# 2) Get the specified date and time , Add and subtract 
time= datetime(2019, 5, 12, 12, 13, 14)
d= time + timedelta(weeks=0, days=0, hours=0, minutes=0, seconds=0, milliseconds=0, microseconds=0, )
# In turn " Zhou " " God ", " when "," branch "," second "," millisecond "," Microsecond "
print(time)
print(d)
time1= "2019-5-12 12:13:14" # character string date 
d1 = datetime.strptime(str(time1),'%Y-%m-%d %H:%M:%S')
plus= d1 + timedelta(days=1) # Add 
minus = d1 - timedelta(days=1) # reduce 
print(time1)
print(d1)
print(plus)
print(minus )
time2= 20190512121314
d2 = datetime.strptime(str(time2),'%Y%m%d%H%M%S')
delta = d2 + timedelta(days=1)
print(time2)
print(d2 )
print(delta)
# 3) date datetime-timestamp Timestamps are transferred to each other 
now_stamp = time.timestamp()
print(' Specify the time corresponding to the timestamp :', now_stamp)
print(' Corresponding to local time :', datetime.fromtimestamp(now_stamp ))
print('UTC Standard time :', datetime.utcfromtimestamp(now_stamp ))
print(' What day of the week :', datetime.fromtimestamp(now_stamp ).weekday())
# 4) datetime Time Convert to str character string 
now = datetime.now()
print(' current time :', now)
print(now.strftime('%Y%m%d%H%M%S'))

Today is my day Python The first of the training camp 20 God , I hope to see the best of you every day

The articles ---- Good article recommends

[Python Public class ] Zero base play Python The basic chapter ---- Section 1 :Python The introduction of

[Python Public class ] Zero base play Python Advanced ---- Section 1 :Python File operations in

Come on, let's have a problem swiping Carnival party Well !----【Python Training camp 】
All right. , That's all I want to share with you today
️️️ If you like , Don't be stingy with your one key triple connection ~


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved