datenum to UTC?

9 Ansichten (letzte 30 Tage)
MrKoberec
MrKoberec am 25 Okt. 2018
Kommentiert: Peter Perkins am 31 Okt. 2018
Hi, I have
10-Oct-2013 15:59:00
and I need UTC time
1381435140
Is there any easy way? (time zone GTM-4)
Thanks!
  1 Kommentar
Guillaume
Guillaume am 25 Okt. 2018
That's not UTC time, that's a Unix Time Stamp
It also appears that your input is a datetime not a datenum.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

James Tursa
James Tursa am 25 Okt. 2018
Bearbeitet: James Tursa am 25 Okt. 2018
Are you just looking for how to convert from the former to the latter? E.g.,
s = '10-Oct-2013 15:59:00';
unix_m4 = (datenum(s) - datenum('01-Jan-1970'))*86400 + 4*3600;
or
unix_m4 = seconds((datetime(s) - datetime('01-Jan-1970')) + seconds(4*3600));
or
unix_m4 = seconds(datetime(s,'timezone','-4') - datetime('01-Jan-1970','timezone','UTC'));
This result isn't called UTC btw, it is called Unix time:
  1 Kommentar
MrKoberec
MrKoberec am 26 Okt. 2018
Thank you, James. Your first suggestion did the job (thank you for including the timezone). I'm using an old version of Matlab (long story) and it does not know the datetime. Sorry I forgot to mention this. Thank you also for the link. I understand it better now.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

jonas
jonas am 25 Okt. 2018
Bearbeitet: jonas am 25 Okt. 2018
Yes, use datetime instead of datenum.
t = datetime('10-Oct-2013 15:59:00','timezone','-04:00')
t_unix = posixtime(t)
  1 Kommentar
Peter Perkins
Peter Perkins am 31 Okt. 2018
'-04:00' is a valid time zone, but perhaps a little dangerous. It's a time zone that does not observe4 DST. If the OP is in, say, Boston, then that would give the wrong answer for 10-Nov. Something like 'America/New_York' is probably the right choice.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by