0

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

?

oshirowanen
  • 15,297
  • 82
  • 198
  • 350

3 Answers3

4

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.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
2

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

joidegn
  • 1,078
  • 9
  • 19
0
var month = date.getMonth();
date.getDate() + "-" + (month >= 10 ? month : '0' + month) + "-" + date.getFullYear();

Read more about Date object in Javascript

a.tereschenkov
  • 807
  • 4
  • 10
  • With his example date that will give him `22-1-1993`. And w3schools is not a good reference, strongly recommend [MDC](https://developer.mozilla.org/en/JavaScript) or the specification. – T.J. Crowder Mar 07 '12 at 14:08