John Cooper
John Cooper

Reputation: 7651

Parsing the date in MM/DD/YY format

I get the response for the Date in this format while showing in the text box, how do i covert it to MM/DD/YYYY and Again re covert it to back to this format while sending

/Date(1306348200000)/

Upvotes: 1

Views: 921

Answers (4)

KooiInc
KooiInc

Reputation: 122986

To convert the regExp-like string to a real Date Object you could use:

var dateNum = Number('/Date(1306348200000)/'.replace(/[^0-9]/g,''))
    , dat = new Date(dateNum); //=>Date {Wed May 25 2011 20:30:00 GMT+0200}

To display formatted dates I use my own small library, which may be of use to you.

Upvotes: 2

mak
mak

Reputation: 13405

var s = '/Date(1306348200000)/';

// convert to javascript date
var date = new Date(parseInt(s.substr(6, 13))); // removes /Date( & )/

// format the date
function pad(n) { return n < 10 ? '0' + n : n; } // leading zeros
var ddmmyy = pad(date.getDate()) + '/' + pad(date.getMonth() + 1) + '/' + date.getFullYear().toString().substr(2);

// convert back
s = '/Date(' + date.getTime() + ')/';

Upvotes: 1

Sergey Metlov
Sergey Metlov

Reputation: 26331

function dateToString(date) {
    return (date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getYear();
}

function dateFromString(str) {
    return new Date(str);
}

Note, that month begins from 0.

Upvotes: 4

blejzz
blejzz

Reputation: 3349

here you can find everything regarding javascript dates http://www.w3schools.com/js/js_obj_date.asp

Upvotes: 0

Related Questions