使用 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()函数用于表示时间差异,例如天、小时、分钟、秒。
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下