如何使用 UliEngineering 在 Python 中生成月份的 datetime 数组
你可以使用 UliEngineering Python 库轻松地生成一个月份范围内的 NumPy datetime64 数组:
generate_months.py
import numpy as np
from UliEngineering.Utils.Date import *
# 生成 2024 年 1 月至 6 月的 datetime 数组
months = generate_months("2024-01-01", "2024-06-30")
print(f"Months Jan-Jun 2024: {len(months)}")
print(f"First month: {months[0]}")
print(f"Last month: {months[-1]}")
# 生成全年的 datetime 数组
months = generate_months("2024-01-01", "2024-12-31")
print(f"\nMonths in a year: {months}")示例输出
generate_months_output.txt
Months Jan-Jun 2024: 6
First month: 2024-01-01
Last month: 2024-06-01
Months in a year: ['2024-01-01' '2024-02-01' '2024-03-01' '2024-04-01' '2024-05-01'
'2024-06-01' '2024-07-01' '2024-08-01' '2024-09-01' '2024-10-01' '2024-11-01'
'2024-12-01']generate_months() 函数返回一个 NumPy datetime64 数组,包含从起始日期(含)到结束日期(含)每个月的第一天。
相关文章
- 如何使用 UliEngineering 在 Python 中生成天数的 datetime 数组
- 如何使用 UliEngineering 在 Python 中生成年份的 datetime 数组
- 如何使用 UliEngineering 在 Python 中从 NumPy datetime64 数组提取月份
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow