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

Posted by thinkaboutit on Tue, 15 Feb 2022 04:28:22 +0100

πŸ“’πŸ“’πŸ“’πŸ“£πŸ“£πŸ“£
🌻🌻🌻 Hello, everyone. My name is Dream. I'm an interesting Python blogger. Please take care of me 😜😜😜
πŸ…πŸ…πŸ… 2021 blog star top 100 2021 blog star top 5, high-quality creator in Python field, welcome to find me for cooperation and learning (VX wants to enter the learning exchange group or learning materials at the end of the article, welcome + + +)
πŸ’• Introduction note: this paradise is never short of genius, and hard work is your final admission ticket! πŸš€πŸš€πŸš€
πŸ’“ Finally, may we all shine where we can't see and make progress together 🍺🍺🍺
πŸ‰πŸ‰πŸ‰ "Ten thousand times sad, there will still be Dream, I have been waiting for you in the warmest place", which is me! Ha ha ha~ 🌈🌈🌈
🌟🌟🌟✨✨✨

[Python training camp] is a topic brushing Carnival party for Python language learning! If you don't grasp the basic knowledge firmly, you are welcome to refer to this set of courses: Python open class It's best to use it together. If you like it, hurry up and subscribe! πŸ‹πŸ‹πŸ‹ If you don't have self-control or motivation to learn and communicate together, please send a private letter or add my VX at the end of the text. I will pull you into the learning and communication group. We will communicate and study together and punch in the group

Title Description

Title Description
This question is a blank filling question. You only need to calculate the result and use the output statement in the code to output the filled result.

How many Mondays are there in the whole 2020 Century (from November 11, 1901 to December 3131, 2000)? Don't tell me you don't know what day it is today

Operational limits
 Maximum running time: 1 s
 Maximum running memory: 128M

Problem solving ideas

  • The date function in the datetime module obtains the date, and obtains the first and last dates respectively: start = date(1901, 1, 1) -end = date(2001, 1, 1)
  • timedelta (1) function, in which 1 is a variable to indicate the meaning of tomorrow, that is, after each cycle, let the date of start change to that of the next day: start += temp

Source sharing

# Time    : 2022/2/13 22:56
# File: Monday py
# Author: it's Dream!
# VX      : Xu18300396393
# Ten thousand times sad, there will still be Dream, I have 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)

***

Some usage of datetime module are as follows:

from datetime import datetime
from datetime import timedelta
import time

# 1) Get current date and time
now_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")	#  Time at this time: mm / DD / yyyy H / min / S
now_time = time.strftime("%Y-%m-%d %H:%M:%S")	#  Time: mm / DD / yyyy
today = datetime.today()    # Returns the current time. The time, minute and second are all 0
print("current time ")
print(today)
today1 = datetime.now()  # Returns the current date and time
now.hour # Time
now.minute # branch
now.isoweekday()# The returned 1-7 represents Monday Sunday;
now.weekday()# The returned 0-6 represents Monday to Sunday
# In the standard format,%w 1-6 means Monday - Saturday, and 0 means Sunday
print( today1 )
today2 = datetime.utcnow()  # The time to return to the current East eighth district is 8 hours less than that at that time
print(today2)


# 2) Gets the specified date and time, plus or minus
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, )		
#They are "week", "day", "hour", "minute", "second", "millisecond" and "microsecond"
print(time)
print(d)

time1= "2019-5-12 12:13:14"		# String date
d1 = datetime.strptime(str(time1),'%Y-%m-%d %H:%M:%S')
plus= d1 + timedelta(days=1)		# plus
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('The specified time corresponds to the timestamp :', now_stamp)

print('Corresponding 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 converted to str string
now = datetime.now()
print('current time  :', now)
print(now.strftime('%Y%m%d%H%M%S'))

πŸ… Today is my 20th day in Python training camp. I hope to see the best of you every day πŸ…

πŸ† Previous articles -- good article recommendation πŸ†

πŸ₯‡ [Python open class] zero foundation playing with Python basics -- Section 1: Python's self introduction

πŸ₯ˆ [Python open class] zero foundation playing with Python advanced chapter -- Section 1: file operation in Python

πŸ₯‰ Come to a topic brushing Carnival party---- [Python training camp]
🌲🌲🌲 Well, that's all I want to share with you today
❀️❀️❀️ If you like it, don't be stingy with your one button three connections~

Topics: Python Back-end