Blog on software engineering
Convert string to date in JavaScript

Convert string to date in JavaScript

If you want to convert a string to a date object in JavaScript and you use jQuery, then you can use the function $.datepicker.parseDate.


The following QUnit test will succeed:

/// <reference path="Scripts/qunit.js"/>
/// <reference path="Scripts/jquery-1.8.3.js"/>
/// <reference path="Scripts/jquery-ui-1.9.2.js"/>
"use strict";

test('JsTest', function ()
    var text = '2012-11-28';
    var expected = new Date(2012, 10, 28); // Note: months start at zero!
    var actual = $.datepicker.parseDate('yy-mm-dd', text);
    ok(expected.getTime() == actual.getTime(),
        'Dates are not equal. Expected:' +
        expected.toISOString() + ' _ Actual:' + actual.toISOString());

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.