使用 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 生日广州男生祝福语简短