1

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)

chonglawr
  • 201
  • 1
  • 3
  • 15
  • the `Date` object has no real string formatting options. you'll have to build it out of smaller parts, e.g. `.getMonth()` – Marc B Aug 26 '16 at 14:57
  • aww man, thanks, that's what i thought – chonglawr Aug 26 '16 at 14:58
  • https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date – Marc B Aug 26 '16 at 15:00
  • 2
    With [momentjs](http://momentjs.com/) you can format the dates as you wish: `moment("2016-08-25T15:17:21.033-10:00").format('DD-MM-YYYY');` – Riccardo Aug 26 '16 at 15:01
  • The question is about is it possible to pass format toUTCString(). The linked question is about formatting general date, not converted to UTC – Michael Freidgeim Jan 05 '22 at 11:43

1 Answers1

3

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());
Thalaivar
  • 23,282
  • 5
  • 60
  • 71