globalization.dateToString

Returns a date formatted as a string according to the client's locale and timezone.

navigator.globalization.dateToString(date, successCallback, errorCallback, options);

Description

Returns the formatted date String via a value property accessible from the object passed as a parameter to the successCallback.

The inbound date parameter should be of type Date.

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

The options parameter is optional, and its default values are:

{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

  • Android
  • BlackBerry WebWorks (OS 5.0 and higher)
  • iOS
  • Windows Phone 8

Quick Example

If the browser is set to the en\_US locale, this displays a popup dialog with text similar to date: 9/25/2012 4:21PM using the default options:

navigator.globalization.dateToString(
    new Date(),
    function (date) { alert('date: ' + date.value + '\n'); },
    function () { alert('Error getting dateString\n'); },
    { formatLength: 'short', selector: 'date and time' }
);

Full Example

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

    function checkDateString() {
      navigator.globalization.dateToString(
        new Date(),
        function (date) {alert('date: ' + date.value + '\n');},
        function () {alert('Error getting dateString\n');,
        {formatLength:'short', selector:'date and time'}}
      );
    }
    </script>
  </head>
  <body>
    <button onclick="checkDateString()">Click for date string</button>
  </body>
</html>

Windows Phone 8 Quirks

  • The formatLength option supports only short and full values.