0

Test Cases

const date1 = 'UTC 2015-08-07 09:13:36'
const dateParse1 = new Date(date1)
console.log(dateParse1.toString()) // Fri Aug 07 2015 09:13:36 GMT+0300 (GMT+03:00)

The date should be parsed as UTC date, but, actually the output is GMT+0300. why is that?

I also tried luxon to parse this date, for example:

const { DateTime } = require('luxon')
const date1 = 'UTC 2015-08-07 09:13:36'
console.log(DateTime.fromISO(date1))

The output:

DateTime {
  ts: 1624865766790,
  _zone: LocalZone {},
  loc: Locale {
    locale: 'en-US',
    numberingSystem: null,
    outputCalendar: null,
    intl: 'en-US',
    weekdaysCache: { format: {}, standalone: {} },
    monthsCache: { format: {}, standalone: {} },
    meridiemCache: null,
    eraCache: {},
    specifiedLocale: null,
    fastNumbersCached: null
  },
  invalid: Invalid {
    reason: 'unparsable',
    explanation: `the input "UTC 2015-08-07 09:13:36" can't be parsed as ISO 8601`
  },
  weekData: null,
  c: null,
  o: null,
  isLuxonDateTime: true
}

I tried other fromXX methods from luxon with no luck.

What is the preferable method to parse and format date in JavaScript?

Note: 'UTC 2015-08-07 09:13:36' date was originally extracted from a video encoded date meta data.

Thanks!

mohjak
  • 101
  • 2
  • 11
  • In Safari, `new Date(date1)` returns an invalid date, see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) Manually parsing the string is trivial: `let [Z, y, m, d, H, M, S] = date1.split(/[\s-:]/); let date = new Date(Date.UTC(y, m-1,d,H,M,S))`. Instead of *split*, you could use `.match(/\w+/g)`. There are many, many questions and answers on [parsing date strings](https://stackoverflow.com/search?q=%5Bjavascript%5D+how+to+parse+a+date). – RobG Jun 29 '21 at 04:09
  • I have to go with manually parsing the date string. Regarding being this question is a similar question, should I delete it? what do you advice. Thank you! – mohjak Jul 01 '21 at 07:10
  • It's already closed as a duplicate, no point in deleting it. – RobG Jul 01 '21 at 08:40

0 Answers0