1

I want to convert my date to this YYYY-MM-DDTHH:MM:SS format. So I tired to use below code,

var date = new Date(Date.UTC(2016, 05, 12, 07, 0, 0));
document.write(date.toISOString());  

But output of this code is

2016-06-12T07:00:00.000Z

Again I removed after last dot index values, finally I got expected output. Is it possible to covert directly with out removing any values?

KSK
  • 636
  • 1
  • 9
  • 29
  • 1
    [Where can I find documentation on formatting a date in JavaScript?](https://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript) – Jonathan Lonowski May 12 '16 at 03:34
  • use `(new Date(2016,5,9,7)).toISOString().replace(/\..+/,'')` or parse each part of the date struct. – Alex Kudryashev May 12 '16 at 03:39

1 Answers1

1

What Jonathan Lonowski mentioned is perfectly fine but you should be using any library like momentjs

//var date = new Date(Date.UTC(2016, 05, 12, 07, 0, 0));
var date = moment("2016-05-12 07:00:00");
console.log(date.format("YYYY-MM-dd[T]HH:mm:SS"));

Or if you are using jQuery, then you can jQuery Date Format

var date = new Date(Date.UTC(2016, 05, 12, 07, 0, 0));
console.log($.format.date(date, "yyyy-MM-ddTHH:mm:SS"));
Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121