“Dapatkan hari terakhir Python” Kode Jawaban

Hari pertama Python bulan lalu

from datetime import date, timedelta

last_day_of_prev_month = date.today().replace(day=1) - timedelta(days=1)

start_day_of_prev_month = date.today().replace(day=1) - timedelta(days=last_day_of_prev_month.day)

# For printing results
print("First day of prev month:", start_day_of_prev_month)
print("Last day of prev month:", last_day_of_prev_month)
Hambo

python datetime hari terakhir bulan

>>import calendar
>>year, month = 2016, 12
>>calendar.monthrange(year, month)[1]
31
Perro Fiel

Dapatkan hari terakhir Python

import datetime
def last_day_of_month(any_day):
	# get close to the end of the month for any day, and add 4 days 'over'
	next_month = any_day.replace(day=28) + datetime.timedelta(days=4)
	# subtract the number of remaining 'overage' days to get last day of current month, or said programattically said, the previous day of the first of next month
	return next_month - datetime.timedelta(days=next_month.day)


last_date_of_month(datetime.date.today())
Modi ji

Jawaban yang mirip dengan “Dapatkan hari terakhir Python”

Pertanyaan yang mirip dengan “Dapatkan hari terakhir Python”

Lebih banyak jawaban terkait untuk “Dapatkan hari terakhir Python” di Python

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya