globalization.stringToDate

Parses a date formatted as a string according to the client's user preferences and calendar using the time zone of the client and returns the corresponding date object.

navigator.globalization.stringToDate(dateString, successCB, errorCB, options);

Description

It returns the date to the success callback with a properties object as a parameter. That object should have the following properties:

  • year {Number}: The four digit year
  • month {Number}: The month from (0 - 11)
  • day {Number}: The day from (1 - 31)
  • hour {Number}: The hour from (0 - 23)
  • minute {Number}: The minute from (0 - 59)
  • second {Number}: The second from (0 - 59)
  • millisecond {Number}: The milliseconds (from 0 - 999), not available on all platforms

The inbound dateString parameter should be of type String.

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.

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

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 "month:8 day:25 year:2012". Note that the month integer is one less than the string, as the month integer represents an index.

navigator.globalization.stringToDate(
  '9/25/2012',
  function (date) {alert('month:' + date.month +
                         ' day:' + date.day + 
                         ' year:' + date.year + '\n');},
  function () {alert('Error getting date\n');},
  {selector:'date'}
);

Full Example

<!DOCTYPE HTML>
<html>
  <head>
    <title>Cordova</title>
    <script type="text/javascript" charset="utf-8" src="cordova-2.3.0.js"></script>
    <script type="text/javascript" charset="utf-8">
              
    function checkStringDate() {
      navigator.globalization.stringToDate(
        '9/25/2012',
        function (date) {alert('month:' + date.month +
                               ' day:' + date.day + 
                               ' year:' + date.year + '\n');},
        function () {alert('Error getting date\n');},
        {selector:'date'}
      );
    }

    </script>
  </head>
  <body>
    <button onclick="checkStringDate()">Click for parsed date</button>
  </body>
</html>

Windows Phone 8 Quirks

  • formatLength option supports only short and full values