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

 

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 input = new Date(2012, 10, 28); // Note: months start at zero!
    var expected = '2012-11-28';
    var actual = $.datepicker.formatDate('yy-mm-dd', input);
    ok(expected == actual,
        'Dates are not equal. Expected:' +
        expected + ' _ Actual:' + actual);
});

Leave a Reply

Your email address will not be published. Required fields are marked *

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