current position:Home>[Django CI system] obtain the current time, the first day and the last day of the month, etc. - 20220510
[Django CI system] obtain the current time, the first day and the last day of the month, etc. - 20220510
2022-05-15 03:37:15【i see the future】
Reference resources
https://blog.csdn.net/TengTesting/article/details/119997493
# Import the package first :
import calendar
import datetime
from datetime import timedelta
# Get today's date :
# return datetime Format :eg:2019-12-07 20:38:35.82816
now = datetime.datetime.now()
# return datetime Format :eg:2019-12-07
now = datetime.datetime.now().date()
now = datetime.date.today()
# Get yesterday's date :
yesterday = now - timedelta(days=1)
# Get tomorrow's date :
tomorrow = now + timedelta(days=1)
# Get the first and last days of the week :
this_week_start = now - timedelta(days=now.weekday())
this_week_end = now + timedelta(days=6-now.weekday())
# Get the first and last day of last week :
last_week_start = now - timedelta(days=now.weekday()+7)
last_week_end = now - timedelta(days=now.weekday()+1)
# Get the first and last days of the month :
this_month_start = datetime.datetime(now.year, now.month, 1)
this_month_end = datetime.datetime(now.year, now.month, calendar.monthrange(now.year, now.month)[1])
# notes :
#calendar.monthrange(year,month)
# Pass in two values : One is the current year , The other is the current month
# It can be written as :calendar.monthrange(now.year,now.month)
# Returns two integers .
# The first value is the day of the week on which the first day of the month is located , Corresponding number .0 - 6==>0( Monday ) To 6( Sunday )
# The second value is the total number of days in the month .
# Get the first and last day of last month :
last_month_end = this_month_start - timedelta(days=1)
last_month_start = datetime.datetime(last_month_end.year, last_month_end.month, 1)
# Get the first and last days of the season :
month = (now.month - 1) - (now.month - 1) % 3 + 1
this_quarter_start = datetime.datetime(now.year, month, 1)
this_quarter_end = datetime.datetime(now.year, month, calendar.monthrange(now.year, now.month)[1])
# Get the first and last days of last season :
last_quarter_end = this_quarter_start - timedelta(days=1)
last_quarter_start = datetime.datetime(last_quarter_end.year, last_quarter_end.month - 2, 1)
# Get the first and last days of the year :
this_year_start = datetime.datetime(now.year, 1, 1)
this_year_end = datetime.datetime(now.year + 1, 1, 1) - timedelta(days=1)
# Get the first and last days of last year :
last_year_end = this_year_start - timedelta(days=1)
last_year_start = datetime.datetime(last_year_end.year, 1, 1)
# Last , If you want to get date The format of , Add... At the end ".date" It can be realized .
My writing
# this_month_start 2022-01-01 00:00:00
# this_month_end 2022-01-31 23:59:59
# last_month_end 2021-12-31 23:59:59
# last_month_start 2021-12-01 00:00:00
# this_quarter_start 2022-01-01 00:00:00
# this_quarter_end 2022-01-31 23:59:59
# last_quarter_end 2021-12-31 23:59:59
# last_quarter_start 2021-10-01 00:00:00
# this_year_start 2022-01-01 00:00:00
# this_year_end 2022-12-31 23:59:59
now = datetime.datetime.now() - timedelta(days=120)
this_month_start = datetime.datetime(now.year, now.month, 1)
#print('this_month_start',this_month_start)
this_month_end = datetime.datetime(now.year, now.month, calendar.monthrange(now.year, now.month)[1]) + timedelta(days=1)- timedelta(seconds=1)
#print('this_month_end',this_month_end)
last_month_end = this_month_start - timedelta(seconds=1)
#print('last_month_end',last_month_end)
last_month_start = datetime.datetime(last_month_end.year, last_month_end.month, 1)
#print('last_month_start',last_month_start)
month = (now.month - 1) - (now.month - 1) % 3 + 1
this_quarter_start = datetime.datetime(now.year, month, 1)
#print('this_quarter_start',this_quarter_start)
this_quarter_end = datetime.datetime(now.year, month, calendar.monthrange(now.year,month)[1]) + timedelta(days=1)- timedelta(seconds=1)
#print('this_quarter_end',this_quarter_end)
last_quarter_end = this_quarter_start - timedelta(days=1)+ timedelta(days=1)- timedelta(seconds=1)
#print('last_quarter_end',last_quarter_end)
last_quarter_start = datetime.datetime(last_quarter_end.year, last_quarter_end.month - 2, 1)
#print('last_quarter_start',last_quarter_start)
this_year_start = datetime.datetime(now.year, 1, 1)
#print('this_year_start',this_year_start)
this_year_end = datetime.datetime(now.year + 1, 1, 1) - timedelta(seconds=1)
#print('this_year_end',this_year_end)
copyright notice
author[i see the future],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/131/202205111536123961.html
The sidebar is recommended
- [brush question Python] the problem of cutting and selling pipes to maximize profits
- Python regular expression
- [Python GUI] wxPython automated data generator practice
- Python script compilation process
- [Python faiss library] (I) Introduction
- Python moves (copies) the pictures (files) under one file to another folder
- Python crawling 51job position information (regular violence matching)
- Python Post Bar irrigation script
- Python simulation QR code login Baidu
- Python calls wechat to send message call through COM port
guess what you like
Analysis of birthday paradox in Python
Equal scale compression of image files in Python
Python dynamic programming (knapsack problem and longest common substring)
Introduction to Python Programming and data analysis (I) basic use of built-in functions
Introduction to Python Programming and data analysis (II) basic use of list derivation
Introduction to Python Programming and data analysis (III) basic use of branch and process control
Python turtle painting Chinese characters
[a quick introduction and comparison of multiple languages -- JavaScript, typescript, python, golang, trust, Java, ruby]
Relevant knowledge of Python web development (I)
python3. Understanding of ID function in 6
Random recommended
- JSON data storage of MySQL in Python
- Python 3 development function
- My pandas
- Python game programming (pyGame)
- Python word cloud
- Python crawler crawls the Douban movie ranking list and writes it into CSV file for visual data analysis
- Python crawler crawls Beijing Xinfadi vegetables and displays them visually
- Python climbed the world university rankings
- Python crawling material commune picture
- Exception handling in Python and explanation of OS module
- Summation of corresponding position elements of multiple lists in Python
- [Python pandas] read excel table contents
- Python zip() function usage
- Application of Python startup subclass subprocess class
- Python's logging module
- MySQL application of Python
- Interesting games designed in Python
- Regularity of Python
- Build Python project in Jenkins, pychar output is normal and Jenkins output modulenotfounderror: no module named problem
- Jenkins reported an error in building Python project: CX_ Oracle. DatabaseError: DPI-1047: oci. dll is not the correct architecture
- Interface request processing of Python webservice
- Hamcrest assertion Library of Python
- Download third-party libraries offline in Python
- Web automation in Python
- Importlib.exe in Python import_ Module import module
- Operation of OS Library in Python
- Some integration operations on Web pages in Python
- Python realizes the super fast window screenshot, automatically obtains the current active window and displays the screenshot
- Implementation of workstation monitoring system with Python socket
- Resume Automation - word 92
- Django foundation -- 02 small project based on Database
- Python drawing word cloud
- Django foundation -- 02 small project based on Database
- MNIST dataset classification based on Python
- Design of FTP client server based on Python
- Signing using RSA algorithm based on Python
- Website backend of online book purchase function based on Python
- Implementation of Tetris game based on Python greedy search
- Django Foundation
- Case: Python weather broadcast system, this is a rainy day