3

I have an object. like this, for example

{
    startDate: 2141242141,
    adress: '',
    endDate: 2141242141,
    billings: {
        startBillingDate: 2142421421421,
        endBillingDate: 2142421421421
    }
    amount: 100
}

I need to get all fields those contain word "Date" (or "date") and convert them all into string date format.

Who can help please.

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
  • What is the formula or algorithm for converting `2141242141` to a Date? Treated as milliseconds since the ECMAScript epoch it's 1970-01-26, treated as seconds it's 2037-11-08. Neither seem appropriate. For conversion to date strings, see [*Where can I find documentation on formatting a date in JavaScript?*](https://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript) – RobG Jul 07 '17 at 21:47

3 Answers3

4

You can loop throught these properties using Object.keys().

And for each property check if its value isn't an object and contains the date string using k.match(/date/i) so change it to a date value. Otherwise if it's an object do the same with its properties in a recursive way.

This is what you need:

function transformProperties(obj) {
  Object.keys(obj).forEach(function(k) {
    if ((typeof obj[k]) !== "object" && k.match(/date/i)) {
      obj[k] = new Date(obj[k]);
    } else if ((typeof obj[k]) == "object") {
      let sub = obj[k];
      transformProperties(sub);
    }
  });
}

Demo:

var o = {
  startDate: 2141242141,
  adress: '',
  endDate: 2141242141,
  billings: {
    startBillingDate: 2142421421421,
    endBillingDate: 2142421421421
  },
  amount: 100
};

function transformProperties(obj) {
  Object.keys(obj).forEach(function(k) {
    if ((typeof obj[k]) !== "object" && k.match(/date/i)) {
      obj[k] = new Date(obj[k]);
    } else if ((typeof obj[k]) == "object") {
      let sub = obj[k];
      transformProperties(sub);
    }
  });
}

transformProperties(o);

console.log(o);
cнŝdk
  • 31,391
  • 7
  • 56
  • 78
0

This recursive function should convert all the dates to ISO strings:

obj = convertDatesToString(obj);

function convertDatesToString(obj) {
    Object.keys(obj).forEach((key) => {
        if(key.toLowerCase().match(/date/)){
            obj[key] = new Date(obj[key]).toISOString();
        } else if (obj[key] instanceof Object) {
            obj[key] = convertDatesToString(obj[key]);
        }
    });

    return obj;
}

obj = {
    startDate: 2141242141,
    adress: '',
    endDate: 2141242141,
    billings: {
        startBillingDate: 2142421421421,
        endBillingDate: 2142421421421
    },
    amount: 100
};

obj = convertDatesToString(obj);
console.log(obj);

function convertDatesToString(obj) {
    Object.keys(obj).forEach((key) => {
        if(key.toLowerCase().match(/date/)){
            obj[key] = new Date(obj[key]).toISOString();
        } else if (obj[key] instanceof Object) {
            obj[key] = convertDatesToString(obj[key]);
        }
    });

    return obj;
}
Teddy Sterne
  • 13,774
  • 2
  • 46
  • 51
0

I would create a function to iterate object's properties recursively, and later I would call the whole function to produce the whole transformations:

function forEachProperty(obj, actionFn) {
  Object.keys(obj).forEach(
    property => {
      actionFn(obj, property);

      if (typeof obj[property] == "object") {
        forEachProperty(obj[property], actionFn);
      }
    }
  );
}

var obj = {
  startDate: 2141242141,
  adress: '',
  endDate: 2141242141,
  billings: {
    startBillingDate: 2142421421421,
    endBillingDate: 2142421421421
  },
  amount: 100
};

forEachProperty(obj, (obj, property) => {
  if (property.toLowerCase().includes("date")) {
    obj[property] = new Date(obj[property]);
  }
});

console.log(JSON.stringify(obj));
Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206