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:15i 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

Random recommended