datetime "PivotYear" doesn't work?

15 vues (au cours des 30 derniers jours)
Richard Livingston
Richard Livingston le 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)

Réponse acceptée

Lei Hou
Lei Hou le 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.

Plus de réponses (0)

Catégories

En savoir plus sur Dates and Time dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by