Search code examples
javascriptdateknockout.jsutc

Getting date in '2015-08-04'{"Y-M-D"} format. How to change the specified date format into UTC format?


I am getting the date format in this way:- '2015-08-04' using knockout.js. How to change the specified date format into UTC format like "2015-08-08T19:20:18+00:00"?

Like in Javascript we are having date function like date.getDate() to get the particular date from the UTC format?

But after getting the date in the above specified format using the knockout.js, I am not able to get the particular date like date.getDate() using JavaScript.

Is there any function in knockout.js to get a date or month or only year from the specified date format?


Solution

  • If you want a Date object, just pass it in through new Date():

    new Date('2015-08-04')
    -> Tue Aug 04 2015 01:00:00 GMT+0100 (GMT Daylight Time)
    

    If you want a UTC string, do the above and call toISOString() on it:

    new Date('2015-08-04').toISOString()
    -> "2015-08-04T00:00:00.000Z"