如何使用 UliEngineering 在 Python 中检查 datetime 是否为每周第一天

你可以使用 UliEngineering Python 库轻松地检查 datetime 值是否为每周的第一天(星期一):

is_first_day_of_week.py
import numpy as np
from UliEngineering.Utils.Date import *

# 检查单个日期
print(f"Is 2024-01-01 first of week? {is_first_day_of_week('2024-01-01')}")
print(f"Is 2024-01-08 first of week? {is_first_day_of_week('2024-01-08')}")

# 检查 NumPy datetime64 数组
dates = np.array(['2024-01-01', '2024-01-02', '2024-01-08', '2024-01-09'], dtype='datetime64[D]')
result = is_first_day_of_week(dates)
print(f"\nFirst of week check: {result}")

示例输出

is_first_day_of_week_output.txt
Is 2024-01-01 first of week? True
Is 2024-01-08 first of week? True

First of week check: [ True False  True False]

is_first_day_of_week() 函数既适用于单个日期,也适用于 NumPy datetime64 数组,对于数组输入会返回布尔数组。

相关文章


Check out similar posts by category: Python, NumPy