-1

How can I convert Javascript Date Objects to a date string in the following format:

Date Object

2000-01-12 23:00:00.000Z

Resulting String

"01/12/2000 23"
ac360
  • 7,735
  • 13
  • 52
  • 91
  • 2
    Can you show us what you have tried? – gdoron Feb 06 '14 at 08:57
  • 1
    Try: `(new Date('2000-01-12 23:00:00.000Z')).toLocaleString()` – tewathia Feb 06 '14 at 08:59
  • 1
    @tewathia - `'2000-01-12 23:00:00.000Z'` isn't a valid date string. – Frogmouth Feb 06 '14 at 09:02
  • This has been asked soooooo many times. See http://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript – user568109 Feb 06 '14 at 09:03
  • @Frogmouth I wouldn't argue with that(I just copied the string from the question), but the Date constructor is incredibly flexible. The code I've written in my comment would work. – tewathia Feb 06 '14 at 09:05
  • @tewathia - for me `.toLocaleString()` is a nice solution. **+1** for this. But form me, with `'2000-01-12 23:00:00.000Z'`, not work. Try yourself, in console get back `Date {Invalid Date}` but the solution (with well formatted date string) is nice. – Frogmouth Feb 06 '14 at 09:09
  • 1
    @Frogmouth I did try it in the console, in Chrome V32. I get `>(new Date('2000-01-12 23:00:00.000Z')).toLocaleString(); >"1/13/2000 4:30:00 AM"`. Also, see http://jsfiddle.net/tewathia/X7LMP/ – tewathia Feb 06 '14 at 09:14
  • 1
    @tewathia ... I believe you, but not seems cross browser :D – Frogmouth Feb 06 '14 at 09:17

1 Answers1

3
date.getDate() + '/' +
(date.getMonth() +1) + '/' + 
date.getFullYear() + ' ' +
(date.getHours() + 1)

Why the +1 you ask? Because javascript. :(

gdoron
  • 147,333
  • 58
  • 291
  • 367