1.概念
日期时间型(Dates and Time)数据具有灵活的显示格式和高达毫微秒的精度,并且可以处理时区、夏令时和平闰年等特殊因素
2.日期时间型数据有以下三种表示方式
(1)Datetime型,表示日期时间点,是存储日期和时间数据的主要方法,它支持算术运算,排序,比较,绘图和格式化显示,datetime数据类型表示一个时间点,例如:2018年4月6日星期五10:00:00;
(2)Duration型,表示日期时间的持续长度,duration数据类型表示时间段,以固定长度为单位的持续时间,例如:72小时10分钟,duration中,1天总是等于24小时,1年总是等于365.2425天;
(3)CalendarDuration型,表示基于日历时间的日期时间的持续长度,calendarDuration表示时间段,以可变长度为单位的持续时间,例如:1个月,可能是28,29,30或31天,calendarDuration数据类型也考虑夏令时和平闰年的变化,使1天可能多于或少于24小时,1年能有365或366天

3. datetime型创建方法
%datetime型,时间点
t = datetime(2018,2,4) %年月日
t = datetime(2018,2,4,16,0,0) %年月日时分秒
t = datetime(2018,2,4,16:17,0,0) %时间序列
运行结果:
t =
datetime
2018-02-04
t =
datetime
2018-02-04 16:00:00
t =
1×2 datetime 数组
2018-02-04 16:00:00 2018-02-04 17:00:00
t=datetime %当前日期时间
t1=datetime('today') %当前日期,日期时间型
t2=date %当前日期,字符型
whos t1 t2
isdatetime(t) %判断是否为该数据类型
运行结果:
t =
datetime
2025-02-16 13:23:43
t1 =
datetime
2025-02-16
t2 =
'16-Feb-2025'
Name Size Bytes Class Attributes
t1 1x1 8 datetime
t2 1x11 22 char
ans =
logical
1
4. duration型创建方法
% duration型,持续时间
d=duration(3,2,1) %时分秒
isduration(d) %判断是否为该数据类型
years(10) %10年
days(10) %10天
运行结果:
d =
duration
03:02:01
ans =
logical
1
ans =
duration
10年
ans =
duration
10天
hours(10) %10小时
minutes(10) %10分钟
seconds(10) %10秒
milliseconds(10) %毫秒
运行结果:
ans =
duration
10小时
ans =
duration
10分钟
ans =
duration
10秒
ans =
duration
0.01秒
5. calendarDuration型创建方法
% calendarDuration型,日历持续时间
c=calendarDuration(3,2,1) %年月日(时分秒)
iscalendarduration(c) %判断是否为该数据类型
运行结果:
c =
calendarDuration
3y 2mo 1d
ans =
logical
1
calyears(10) %日历年
calquarters(10) %日历季度
calmonths(10) %日历月
运行结果:
ans =
calendarDuration
10y
ans =
calendarDuration
10q
ans =
calendarDuration
10mo
calweeks(10) %日历周
caldays(10) %日历天
运行结果:
ans =
calendarDuration
10w
ans =
calendarDuration
10d
6. NaT非时间(Not-a-Time),与数值型中的nan类似
t=NaT
isnat(datetime) %判断NaT(Not-a-Time)元素
isnat(t)
运行结果:
t =
datetime
NaT
ans =
logical
0
ans =
logical
1
免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删