oshirowanen
oshirowanen

Reputation: 15965

Date formatting

Using the following:

var date = new Date(parseInt(jsonDate.substr(6)));

I get:

Mon Feb 22 1993 00:00:00 GMT+0000 (GMT Standard Time)

How do I format this too

22-02-1993

?

Upvotes: 0

Views: 261

Answers (3)

a.tereschenkov
a.tereschenkov

Reputation: 807

var month = date.getMonth();
date.getDate() + "-" + (month >= 10 ? month : '0' + month) + "-" + date.getFullYear();

Read more about Date object in Javascript

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1075337

You use the getFullYear, getMonth (note that the values start with 0), and getDate functions on the Date instance, then assemble a string. (Those links are to the specification, which can be hard to read; MDC does a bit better.)

Or use a library like DateJS (although it hasn't been maintained in quite some time) or as joidegn mentions, moment.js.

Upvotes: 4

joidegn
joidegn

Reputation: 1068

momentjs works nicely. NAtive Javascript unfortunately lacks somewhat in this regard although You could cocatenate the date elements together.

Upvotes: 2

Related Questions