使用 Numpy 打印今天、昨天和明天的日期
在这个程序中,我们将使用numpy库打印今天、昨天和明天的日期。
算法
Step 1: Import the numpy library. Step 2: find today's date using the datetime64() function. Step 3: find yesterday's date by subtracting the output of timedelta64() function from the output of datetime64() function. Step 4: Find yesterday's date by adding the output of timedelta64() function from the output of datetime64() function.
示例代码
import numpy as np
todays_date = np.datetime64('today', 'D')
print("Today's Date: ", todays_date)
yesterdays_date = np.datetime64('today', 'D') - np.timedelta64(1, 'D')
print("Yesterday's Date: ", yesterdays_date)
tomorrows_date = np.datetime64('today', 'D') + np.timedelta64(1, 'D')
print("Tomorrow's Date: ", tomorrows_date)输出结果Today's Date: 2021-02-16 Yesterday's Date: 2021-02-15 Tomorrow's Date: 2021-02-17
解释
在numpy中,有一些数据类型支持日期时间功能。为该函数指定名称“datetime64”,因为名称“datetime”已被Python中的库使用。
datetime64()函数中的“D”参数用于以“天”为单位获取日期。timedelta64()函数用于表示时间差异,例如天、小时、分钟、秒。
热门推荐
10 读书祝福语女生短句简短
11 感谢同事新婚祝福语简短
12 感谢教官文案祝福语简短
13 员工聚会蛋糕祝福语简短
14 元宵发客户祝福语简短
15 给团队信心祝福语简短
16 医美的祝福语大全简短
17 异国姻缘的祝福语简短
18 兄妹拍照祝福语简短的话