Why aren't my locale times converted to UTC time?

조회 수: 1 (최근 30일)
Javier
Javier 2018년 4월 20일
댓글: Peter Perkins 2018년 4월 26일
Hello all,
I would like to convert some dates that are taken with the Amsterdam TimeZone into UTC TimeZone, reading Matlab doc I should use datetime combined wit the the TimeZone option. I have done so, but my time once converted keeps staying in the Amsterdam local time, I know that it should be two hours less in UTC.
Here below the proccess I use, myDatesLocal are Amsterdam timezone based, so array b should stay the same as it does, however, array a should change to two ours earlier. I would expect somehting like 12-Apr-2018 12:51:36 for the first date.
K>> myDatesLocal = myDates(1:3)
myDatesLocal =
3×1 cell array
'12/04/18 14:51:36'
'12/04/18 14:52:36'
'12/04/18 14:53:36'
K>> b =datetime(myDatesLocal,'InputFormat','dd/MM/yy HH:mm:ss','TimeZone','Europe/Amsterdam')
b =
3×1 datetime array
12-Apr-2018 14:51:36
12-Apr-2018 14:52:36
12-Apr-2018 14:53:36
K>> a =datetime(myDatesLocal,'InputFormat','dd/MM/yy HH:mm:ss','TimeZone','UTC')
a =
3×1 datetime array
12-Apr-2018 14:51:36
12-Apr-2018 14:52:36
12-Apr-2018 14:53:36
What am I doing wrong?
Thanks in advance!

채택된 답변

Guillaume
Guillaume 2018년 4월 20일

No, when you create your a, you're simply telling matlab that the 14:51 is measured in the UTC timezone. At no point do you tell matlab that it is measured in Amsterdam timezone but to be displayed in UTC.

The correct way to do what you want is to create your datetime in the Amsterdam timezone, then tell matlab to change the timezone to UTC:

>> myDatesLocal = {'12/04/18 14:51:36'
                   '12/04/18 14:52:36'
                   '12/04/18 14:53:36'};
>> b = datetime(myDatesLocal,'InputFormat','dd/MM/yy HH:mm:ss','TimeZone','Europe/Amsterdam')
b = 
  3×1 datetime array
   12-Apr-2018 14:51:36
   12-Apr-2018 14:52:36
   12-Apr-2018 14:53:36
>> b.TimeZone = 'UTC'
b = 
  3×1 datetime array
   12-Apr-2018 12:51:36
   12-Apr-2018 12:52:36
   12-Apr-2018 12:53:36
  댓글 수: 4
Javier
Javier 2018년 4월 24일
Thanks Peter, could you explain a bit more what time zone I should specify in with the z option?
Peter Perkins
Peter Perkins 2018년 4월 26일
I don't understand the question. 'z' is something you would add to the display format of a datetime to show the (offset of) the time zone your array is set to. doc datetime.Format.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by