使用 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()函数用于表示时间差异,例如天、小时、分钟、秒。
热门推荐
6 保研的祝福语简短
10 年轻20岁祝福语简短
11 朋友结婚祝福语信息简短
12 女孩婚礼贺卡祝福语简短
13 30段点歌简短祝福语
14 虎年春节祝福语图文简短
15 写给后妈祝福语大全简短
16 简短回复生日祝福语
17 校长送毕业祝福语简短
18 毕业立体贺卡祝福语简短