How can I get a (MM-DD-YYYY) format from a UTC string in javascript?

chonglawr picture chonglawr · Aug 26, 2016 · Viewed 11.5k times · Source

I am working with a Date object in a different timezone than the console. I'm using utc string to get the actual time that was logged in that timezone. Is there an easy way to extract the (MM-DD-YYYY) from the utc string than just substringing into the string?

isoString = "2016-08-25T15:17:21.033-10:00"
utc= (new Date(isoString)).toUTCString()

Returns: "Fri, 26 Aug 2016 01:17:21 GMT"

Would like (08-26-2016)

Answer

Thalaivar picture Thalaivar · Aug 26, 2016

You would convert it into a Date Object and then try out the methods available in it to get the required format.

var isoString = "2016-08-25T15:17:21.033-10:00"
var utc= (new Date(isoString)).toUTCString()

console.log(new Date(utc).getDate() + "-" + (new Date(utc).getMonth() + 1) +
                                                 "-" + new Date(utc).getFullYear());