datetime "PivotYear" doesn't work?

조회 수: 12 (최근 30일)
Richard Livingston
Richard Livingston 2019년 9월 23일
답변: Lei Hou 2019년 9월 23일
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