HTMLMediaElement: getStartDate() method

The getStartDate() method of the HTMLMediaElement interface returns a new Date object representing the real-world date and time corresponding to the beginning of the media.

This is useful for media streams that are anchored to a real-world clock, such as a live broadcast that began at a specific date and time. For media that does not include date and time information, the returned Date object will have a time value of NaN.

Syntax

js
getStartDate()

Parameters

None.

Return value

A Date object representing the start date and time of the media. If the media does not include date and time information, the returned Date object will have a time value of NaN.

Description

Internally, each media element tracks a start date, which begins as NaN (not set). Once the browser has loaded enough data to read the media's metadata, it sets the start date to the real-world time that corresponds to the beginning of the media — if the format provides one. If it doesn't, the start date stays NaN.

For media that does specify a start time and date (for example, a live TV broadcast streamed over the web), getStartDate() returns a Date object corresponding to the real-world time at which the media begins. This allows media player controls to display absolute times (such as "2:30 PM") rather than times relative to the start of playback (such as "3 hours, 12 minutes").

The returned Date will have a time value of NaN (making it an invalid date) in either of the following cases:

  • No data has been loaded yet (readyState is HAVE_NOTHING), so the start date hasn't been set.
  • The media format doesn't include date and time information.

The start date is not guaranteed to be available as soon as the loadedmetadata event fires. For example, HLS streams carry dates in segment-level #EXT-X-PROGRAM-DATE-TIME tags, which may not have been read yet at that point. Listening for the loadeddata event instead is more reliable across formats, because by then the browser has loaded enough data to determine the start date.

Examples

Displaying the start date of a live stream

This example retrieves the start date of a live stream — the real-world date and time at which the broadcast began, as embedded in the stream by the server — and displays it. It listens for the loadeddata event, which fires once enough data has been loaded for the start date to be available.

HTML

html
<video src="livestream.m3u8" controls></video>
<output>Start date: loading…</output>

JavaScript

js
const video = document.querySelector("video");
const display = document.querySelector("output");

video.addEventListener("loadeddata", () => {
  const startDate = video.getStartDate();

  if (isNaN(startDate.getTime())) {
    display.textContent = "Start date: not available";
  } else {
    display.textContent = `Start date: ${startDate.toLocaleString()}`;
  }
});

Result

The output below shows the start date of the media, as provided by the server. Note that this is encoded in the example metadata in stream.m3u8.

Specifications

Specification
HTML
# dom-media-getstartdate

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
getStartDate
Chrome – No support
Chrome
footnote No support
Edge – No support
Edge
footnote No support
Firefox – No support
Firefox
footnote No support
Opera – No support
Opera
footnote No support
Safari – Full support
Safari 9 (Release date: 2015-09-30)
footnote Full support
Chrome Android – No support
Chrome Android
footnote No support
Firefox for Android – No support
Firefox for Android
footnote No support
Opera Android – No support
Opera Android
footnote No support
Safari on iOS – Full support
Safari on iOS 9 (Release date: 2015-09-16)
footnote Full support
Samsung Internet – No support
Samsung Internet
footnote No support
WebView Android – No support
WebView Android
footnote No support
WebView on iOS – Full support
WebView on iOS 9 (Release date: 2015-09-16)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support

See also