1

I am making a Restful call which is returning me Json and dates are coming in a weird format which looks like following:

/Date(-62135568000000)/

What is easiest way to make it look normal like (January 10, 2016)?

I did come across some of the articles but everything was requiring me to write some regex function. I am thinking that this must be a common problem and there has to be an easy one-liner in Java-script. Any ideas?

Lost
  • 12,007
  • 32
  • 121
  • 193

1 Answers1

1

Not sure how that date is parsed, I couldn't get it anywhere near 2016, but you could format it like this

const date = '/Date(-62135568000000)/'

const zeroify = num => num < 10 ? '0' + num : num

const monthify = month => {
  const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', '...']
  return months[month]
}

function parseDate(dateStr) {
  const date = new Date(parseInt(dateStr.match(/(\d+)/)[1]) / 100)
  const parts = [
    monthify(date.getMonth()), ' ', 
    zeroify(date.getDate()), ', ',
    date.getFullYear()
  ]
  return '(' + parts.join('') + ')'
}

console.log(
  parseDate(date)
)
synthet1c
  • 6,152
  • 2
  • 24
  • 39