globalization.dateToString

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

navigator.globalization.dateToString(date, successCB, errorCB, options);

Description

It returns the formatted date string to the successCB callback with a properties object as a parameter. That object should have a value property with a String value.

The inbound date parameter should be of type Date.

If there is an error formatting the date, then the errorCB callback is invoked with a GlobalizationError object as a parameter. The expected code for this error is GlobalizationError.FORMATTING_ERROR.

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

The default options are {formatLength:'short', selector:'date and time'}. The options parameter is optional.

Supported Platforms

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

Quick Example

In the case when the browser is set to the en_US locale, this should display 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>Cordova</title>
    <script type="text/javascript" charset="utf-8" src="cordova-2.4.0.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

  • formatLength option supports only short and full values