Main Content

# Convert Date and Time to Julian Date or POSIX Time

You can convert `datetime` arrays to represent points in time in specialized numeric formats. In general, these formats represent a point in time as the number of seconds or days that have elapsed since a specified starting point. For example, the Julian date is the number of days and fractional days that have elapsed since the beginning of the Julian period. The POSIX® time is the number of seconds that have elapsed since 00:00:00 1-Jan-1970 UTC (Universal Coordinated Time). MATLAB® provides the `juliandate` and `posixtime` functions to convert `datetime` arrays to Julian dates and POSIX times.

While `datetime` arrays are not required to have a time zone, converting "unzoned" `datetime` values to Julian dates or POSIX times can lead to unexpected results. To ensure the expected result, specify the time zone before conversion.

### Specify Time Zone Before Conversion

You can specify a time zone for a `datetime` array, but you are not required to do so. In fact, by default the `datetime` function creates an "unzoned" `datetime` array.

Create a `datetime` value for the current date and time.

`d = datetime('now')`
```d = datetime 23-Feb-2021 14:50:27 ```

`d` is constructed from the local time on your machine and has no time zone associated with it. In many contexts, you might assume that you can treat the times in an unzoned `datetime` array as local times. However, the `juliandate` and `posixtime` functions treat the times in unzoned `datetime` arrays as UTC times, not local times. To avoid any ambiguity, it is recommended that you avoid using `juliandate` and `posixtime` on unzoned `datetime` arrays. For example, avoid using `posixtime(datetime('now'))` in your code.

If your `datetime` array has values that do not represent UTC times, specify the time zone using the `TimeZone` name-value pair argument so that `juliandate` and `posixtime` interpret the `datetime` values correctly.

`d = datetime('now','TimeZone','America/New_York')`
```d = datetime 23-Feb-2021 14:50:27 ```

As an alternative, you can specify the `TimeZone` property after you create the array.

`d.TimeZone = 'America/Los_Angeles'`
```d = datetime 23-Feb-2021 11:50:27 ```

To see a complete list of time zones, use the `timezones` function.

### Convert Zoned and Unzoned Datetime Values to Julian Dates

A Julian date is the number of days (including fractional days) since noon on November 24, 4714 BCE, in the proleptic Gregorian calendar, or January 1, 4713 BCE, in the proleptic Julian calendar. To convert `datetime` arrays to Julian dates, use the `juliandate` function.

Create a `datetime` array and specify its time zone.

```DZ = datetime('2016-07-29 10:05:24') + calmonths(1:3); DZ.TimeZone = 'America/New_York'```
```DZ = 1x3 datetime 29-Aug-2016 10:05:24 29-Sep-2016 10:05:24 29-Oct-2016 10:05:24 ```

Convert `D` to the equivalent Julian dates.

```format longG JDZ = juliandate(DZ)```
```JDZ = 1×3 2457630.08708333 2457661.08708333 2457691.08708333 ```

Create an unzoned copy of `DZ`. Convert `D` to the equivalent Julian dates. As `D` has no time zone, `juliandate` treats the times as UTC times.

```D = DZ; D.TimeZone = ''; JD = juliandate(D)```
```JD = 1×3 2457629.92041667 2457660.92041667 2457690.92041667 ```

Compare `JDZ` and `JD`. The differences are equal to the time zone offset between UTC and the `America/New_York` time zone in fractional days.

`JDZ - JD `
```ans = 1×3 0.166666666511446 0.166666666511446 0.166666666511446 ```

### Convert Zoned and Unzoned Datetime Values to POSIX Times

The POSIX time is the number of seconds (including fractional seconds) elapsed since 00:00:00 1-Jan-1970 UTC (Universal Coordinated Time), ignoring leap seconds. To convert `datetime` arrays to POSIX times, use the `posixtime` function.

Create a `datetime` array and specify its time zone.

```DZ = datetime('2016-07-29 10:05:24') + calmonths(1:3); DZ.TimeZone = 'America/New_York'```
```DZ = 1x3 datetime 29-Aug-2016 10:05:24 29-Sep-2016 10:05:24 29-Oct-2016 10:05:24 ```

Convert `D` to the equivalent POSIX times.

`PTZ = posixtime(DZ)`
```PTZ = 1×3 1472479524 1475157924 1477749924 ```

Create an unzoned copy of `DZ`. Convert `D` to the equivalent POSIX times. As `D` has no time zone, `posixtime` treats the times as UTC times.

```D = DZ; D.TimeZone = ''; PT = posixtime(D)```
```PT = 1×3 1472465124 1475143524 1477735524 ```

Compare `PTZ` and `PT`. The differences are equal to the time zone offset between UTC and the `America/New_York` time zone in seconds.

`PTZ - PT`
```ans = 1×3 14400 14400 14400 ```

Download ebook
{"ft_lang_label":"__label__en","ft_lang_prob":0.6456224,"math_prob":0.969907,"size":4186,"snap":"2021-04-2021-17","text_gpt3_token_len":1209,"char_repetition_ratio":0.15471067,"word_repetition_ratio":0.24085365,"special_character_ratio":0.31318682,"punctuation_ratio":0.13268998,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96574324,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-04-12T16:20:55Z\",\"WARC-Record-ID\":\"<urn:uuid:4ac5e67a-689e-4ee8-b19c-48aead8355bd>\",\"Content-Length\":\"78917\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:713bb826-f255-4579-83c6-2e5dea05427c>\",\"WARC-Concurrent-To\":\"<urn:uuid:6a2c25da-88e1-4d19-8c8a-9e157446c4ad>\",\"WARC-IP-Address\":\"104.117.39.124\",\"WARC-Target-URI\":\"https://www.mathworks.com/help/matlab/matlab_prog/specify-time-zone-for-conversion-to-julian-date-or-posix-time.html\",\"WARC-Payload-Digest\":\"sha1:4BZZ4ETRFAKMZVXYUX7GSALKCVANLR5C\",\"WARC-Block-Digest\":\"sha1:XGQH5NQ7DSY77U7UBNZ5ZJ45JWYZXPS4\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-17/CC-MAIN-2021-17_segments_1618038067870.12_warc_CC-MAIN-20210412144351-20210412174351-00011.warc.gz\"}"}