Hình ảnh của Towfiqu Barbhuiya, Unplash.
4 phút để đọc
Khi tạo báo cáo kinh doanh hoặc chạy các truy vấn đối với nền tảng cơ sở dữ liệu hoặc phân tích web trong cài đặt kinh doanh, bạn sẽ thường cần biết ngày bắt đầu và kết thúc của tháng.
Trong dự án nhanh này, chúng tôi sẽ tạo ra một vài chức năng giúp chúng tôi tính toán ngày bắt đầu và ngày kết thúc của tháng, để bạn có thể chuyển các giá trị cho các truy vấn của mình để trích xuất dữ liệu chính xác.
Nhập các gói
Đầu tiên, mở một máy tính xách tay Jupyter và nhập các gói mà chúng tôi sẽ cần. Chúng ta có thể sử dụng ba mô -đun từ thư viện Python Standard datetime
cho điều này: date
, datetime
và
def get_first_date_of_current_month[year, month]:
"""Return the first date of the month.
Args:
year [int]: Year
month [int]: Month
Returns:
date [datetime]: First date of the current month
"""
first_date = datetime[year, month, 1]
return first_date.strftime["%Y-%m-%d"]
0.from datetime import date, datetime, timedelta
Tính ngày vào đầu tháng
Tiếp theo, chúng tôi sẽ tạo một chức năng để trả về ngày bắt đầu của tháng theo định dạng ngày Yyyy-MM-DD thông thường được sử dụng trong các cơ sở dữ liệu như MySQL. Để làm điều này, chúng tôi sẽ vượt qua trong
def get_first_date_of_current_month[year, month]:
"""Return the first date of the month.
Args:
year [int]: Year
month [int]: Month
Returns:
date [datetime]: First date of the current month
"""
first_date = datetime[year, month, 1]
return first_date.strftime["%Y-%m-%d"]
1 và def get_first_date_of_current_month[year, month]:
"""Return the first date of the month.
Args:
year [int]: Year
month [int]: Month
Returns:
date [datetime]: First date of the current month
"""
first_date = datetime[year, month, 1]
return first_date.strftime["%Y-%m-%d"]
2 với tư cách là số nguyên, sau đó trả lại một đối tượng datetime
bằng cách đặt ngày lên 1.Sau đó, chúng tôi sẽ chuyển đổi đối tượng datetime
thành một chuỗi ở định dạng
def get_first_date_of_current_month[year, month]:
"""Return the first date of the month.
Args:
year [int]: Year
month [int]: Month
Returns:
date [datetime]: First date of the current month
"""
first_date = datetime[year, month, 1]
return first_date.strftime["%Y-%m-%d"]
5 bằng def get_first_date_of_current_month[year, month]:
"""Return the first date of the month.
Args:
year [int]: Year
month [int]: Month
Returns:
date [datetime]: First date of the current month
"""
first_date = datetime[year, month, 1]
return first_date.strftime["%Y-%m-%d"]
6.def get_first_date_of_current_month[year, month]:
"""Return the first date of the month.
Args:
year [int]: Year
month [int]: Month
Returns:
date [datetime]: First date of the current month
"""
first_date = datetime[year, month, 1]
return first_date.strftime["%Y-%m-%d"]
Bây giờ chúng ta có thể chạy chức năng
def get_first_date_of_current_month[year, month]:
"""Return the first date of the month.
Args:
year [int]: Year
month [int]: Month
Returns:
date [datetime]: First date of the current month
"""
first_date = datetime[year, month, 1]
return first_date.strftime["%Y-%m-%d"]
7 để có được ngày đầu tiên của tháng hiện tại, chỉ đơn giản là bằng cách trôi qua trong năm và tháng.first = get_first_date_of_current_month[2022, 1]
first
first = get_first_date_of_current_month[2025, 5]
first
Tính ngày vào cuối tháng
Tính toán ngày kết thúc tháng khó hơn một chút, vì không giống như ngày đầu tiên của tháng, luôn luôn là 1, ngày cuối cùng trong tháng có thể là 31, 30, 28 hoặc 29 tùy theo tháng hoặc năm.
Có một vài cách để làm điều này, nhưng tôi đã đi với những điều sau đây:
def get_first_date_of_current_month[year, month]:
"""Return the first date of the month.
Args:
year [int]: Year
month [int]: Month
Returns:
date [datetime]: First date of the current month
"""
first_date = datetime[year, month, 1]
return first_date.strftime["%Y-%m-%d"]
8.Điều này sẽ tính toán ngày đầu tiên của tháng sau, sau đó trừ 1 ngày từ đó để có được ngày cuối cùng của tháng hiện tại. Mô -đun
def get_first_date_of_current_month[year, month]:
"""Return the first date of the month.
Args:
year [int]: Year
month [int]: Month
Returns:
date [datetime]: First date of the current month
"""
first_date = datetime[year, month, 1]
return first_date.strftime["%Y-%m-%d"]
0 là tuyệt vời để tính toán sự khác biệt giữa ngày.def get_last_date_of_month[year, month]:
"""Return the last date of the month.
Args:
year [int]: Year, i.e. 2022
month [int]: Month, i.e. 1 for January
Returns:
date [datetime]: Last date of the current month
"""
if month == 12:
last_date = datetime[year, month, 31]
else:
last_date = datetime[year, month + 1, 1] + timedelta[days=-1]
return last_date.strftime["%Y-%m-%d"]
last = get_last_date_of_month[2022, 1]
last
last = get_last_date_of_month[2022, 4]
last
Cảm ơn Ron Burak vì đã điều chỉnh.
Matt Clarke, Thứ Sáu, ngày 18 tháng 3 năm 2022