0

I'm rendering JSON dates that look like this: /Date(1185336000000)/ and I'm trying to convert them to a ("mmmm d, yyyy")-type format.

I tried a if...includes() and then a format() method, but they didn't work (bad syntax) and they disabled already rendered data.

JS snippet:

import firmData from '../JSON/firmDir.json';

function loadBio() {

let dateOfHire = firmData.d.results.map(function(val) {
      // if (val.hiredate === null || undefined) return " ";
      let newDate = val.hiredate;
      if (newDate.includes('000')) {
           newDate.format("mmmm d, yyyy");
      }
    return val.hiredate
 })

$("#hireDate-id").append(dateOfHire);

}

loadBio()

JSON snippet:

{
  "d": {
    "results": [
      {
       "hiredate": "/Date(1185336000000)/",
       ...
Bodrov
  • 653
  • 5
  • 21

1 Answers1

0

Hey I just created this function - maybe this helps a bit to solve your problem?

const date = "/Date(1185336000000)/";
function format(date:string) {
    const matches = date.match(/\d+/);
    const ts = matches[0]; 
    const dateObj = new Date(ts*1);
    const day = dateObj.getDay();
    const month = dateObj.getMonth();
    const year = dateObj.getFullYear();

    return day, month, year; // returns 3 6 2007
}
davbuc
  • 487
  • 5
  • 12