datetime "PivotYear" doesn't work?

12 次查看(过去 30 天)
I am trying to use datetime() to convert string dates to datenum, to use as an axis for plotting data. My input is (from debugger session):
K>> ttemp
ttemp =
10×1 string array
"8/20/19 00:00"
"8/20/19 00:01"
"8/20/19 00:02"
"8/20/19 00:03"
"8/20/19 00:04"
"8/20/19 00:05"
"8/20/19 00:06"
"8/20/19 00:07"
"8/20/19 00:08"
"8/20/19 00:09"
If I convert with datetime I get:
K>> dt = datetime(ttemp)
dt =
10×1 datetime array
20-Aug-0019 00:00:00
20-Aug-0019 00:01:00
20-Aug-0019 00:02:00
20-Aug-0019 00:03:00
20-Aug-0019 00:04:00
20-Aug-0019 00:05:00
20-Aug-0019 00:06:00
20-Aug-0019 00:07:00
20-Aug-0019 00:08:00
20-Aug-0019 00:09:00
The years are supposed to be 2019, and I'd like to fix that, so I tried to use the 'PivotYear' name/value pair:
K>> dt = datetime(ttemp,'PivotYear',2000)
dt =
10×1 datetime array
20-Aug-0019 00:00:00
20-Aug-0019 00:01:00
20-Aug-0019 00:02:00
20-Aug-0019 00:03:00
20-Aug-0019 00:04:00
20-Aug-0019 00:05:00
20-Aug-0019 00:06:00
20-Aug-0019 00:07:00
20-Aug-0019 00:08:00
20-Aug-0019 00:09:00
Why doesn't this work?
I'm using Matlab Version 9.6.0.1174912 (R2019a)

采纳的回答

Lei Hou
Lei Hou 2019-9-23
Hi Richard,
The pivot year has an effect only when the 'InputFormat' parameter is specified and it includes y or yy. Therefore, to solve your problem, you can use:
dt = datetime(ttemp,'InputFormat','M/dd/yy hh:mm','PivotYear',2000)
I'll contact our documentation team to improve our datetime documentation.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Calendar 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by