globalization.getDatePattern

Returns a pattern string to format and parse dates according to the client's user preferences.

navigator.globalization.getDatePattern(successCallback, errorCallback, options);

Description

Returns the pattern to the successCallback. The object passed in as a parameter contains the following properties:

  • pattern: The date and time pattern to format and parse dates. The patterns follow Unicode Technical Standard #35. (String)

  • timezone: The abbreviated name of the time zone on the client. (String)

  • utc_offset: The current difference in seconds between the client's time zone and coordinated universal time. (Number)

  • dst_offset: The current daylight saving time offset in seconds between the client's non-daylight saving's time zone and the client's daylight saving's time zone. (Number)

If there is an error obtaining the pattern, the errorCallback executes with a [GlobalizationError](GlobalizationError/globalizationerror.html) object as a parameter. The error's expected code is [GlobalizationError](GlobalizationError/globalizationerror.html).PATTERN\_ERROR.

The options parameter is optional, and defaults to the following values:

{formatLength:'short', selector:'date and time'}

The options.formatLength can be short, medium, long, or full. The options.selector can be date, time or date and time.

Supported Platforms

  • Amazon Fire OS
  • Android
  • BlackBerry WebWorks 5.0+
  • iOS
  • Windows Phone 8

Quick Example

When the browser is set to the en\_US locale, this example displays a popup dialog with text such as pattern: M/d/yyyy h:mm a:

function checkDatePattern() {
    navigator.globalization.getDatePattern(
        function (date) { alert('pattern: ' + date.pattern + '\n'); },
        function () { alert('Error getting pattern\n'); },
        { formatLength: 'short', selector: 'date and time' }
    );
}

Full Example

<!DOCTYPE HTML>
<html>
  <head>
    <title>getDatePattern Example</title>
    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script type="text/javascript" charset="utf-8">

    function checkDatePattern() {
      navigator.globalization.getDatePattern(
        function (date) {alert('pattern: ' + date.pattern + '\n');},
        function () {alert('Error getting pattern\n');},
        {formatLength:'short', selector:'date and time'}
      );
    }

    </script>
  </head>
  <body>
    <button onclick="checkDatePattern()">Click for pattern</button>
  </body>
</html>

Windows Phone 8 Quirks

  • The formatLength supports only short and full values.

  • The pattern for date and time pattern returns only full datetime format.

  • The timezone returns the full time zone name.

  • The dst_offset property is not supported, and always returns zero.