Using now() in different time zones (or with daylight saving time)
16 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Neil Rogers
am 6 Dez. 2019
Bearbeitet: Neil Rogers
am 9 Dez. 2019
If two people in two different time zones simultaneously used the now() function, would they get different answers? Also, does the now() result depend on whether daylight saving time is in use?
I understand now() returns the number of days since 0 Jan 0000, but does that mean "0 Jan 0000 UTC", or "0 Jan 0000 in the local time zone"?
Many thanks,
Neil
2 Kommentare
Akzeptierte Antwort
Steven Lord
am 6 Dez. 2019
If you want control over the time zone, you probably want to use datetime instead of now.
N1 = datetime('now', 'TimeZone', 'local')
N2 = datetime('now', 'TimeZone', 'America/New_York')
N3 = datetime('now', 'TimeZone', 'Europe/London')
See this documentation page for more information about working with dates and times with time zones. If you want to know if a datetime occurs during Daylight Savings Time, use isdst. When called on datetime arrays without a time zone set, isdst return false.
3 Kommentare
Steven Lord
am 6 Dez. 2019
The timezones function output for the "Etc" Area shows that Etc/UTC has 0 UTCOffset and 0 DSTOffset, so I think you're fine.
t = timezones('Etc')
Weitere Antworten (1)
Jeremy
am 6 Dez. 2019
now
Limitations
- MATLAB Online returns the current date and time in Coordinated Universal Time (UTC) rather than local time.
1 Kommentar
Jeremy
am 6 Dez. 2019
Additionally, you can use the syntax
[c tf] = clock
Where tf will be a logical that is 1 (true) if the current local date and time occur during Daylight Saving Time in your current time zone, and 0 (false) otherwise.
Siehe auch
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!