0

Hello my user can input date like 2/2/2008, but I need it in format 02/02/2008. How do i reformat it with date.js?

I have tried:

document.getElementById("smth").value = Date.parse(smth, "dd/mm/yyyy").toString("dd/mm/yyyy");

while parse works fine, toString outputs 02/00/2008? and if run it again on date 02/00/2008 I get 01/00/2008.

Can anyone point me in right direction?

niproblema
  • 91
  • 1
  • 10
  • possible duplicate of [Where can I find documentation on formatting a date in JavaScript](http://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript) – Teemu Oct 23 '14 at 07:01
  • And not any of [these 41 answers](http://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript/10119138#10119138) was what you want? – Teemu Oct 23 '14 at 07:02
  • Sorry bout that. I searched few answers but I hadn't come across that one. Solution was MM instead of mm. Check the solution below – niproblema Oct 23 '14 at 07:04

2 Answers2

1

Isn't this case sensitive? mm for minutes, MM for months?

MeanGreen
  • 3,098
  • 5
  • 37
  • 63
1

try to format with "dd/MM/yyyy"

Date.parse("2/2/2008", "dd/mm/yyyy").toString("dd/MM/yyyy")

hijeane
  • 66
  • 1
  • 4