MATLAB Answers

datetime "PivotYear" doesn't work?

6 views (last 30 days)
Richard Livingston
Richard Livingston on 23 Sep 2019
Answered: Lei Hou on 23 Sep 2019
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)

Accepted Answer

Lei Hou
Lei Hou on 23 Sep 2019
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.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by