0

I'm trying to format a date inside an object.

I'm currently have the next array of objects:

    [
      { date: 2022-01-03T05:00:41.560Z },
      { date: 2022-01-03T22:54:33.980Z },
      { date: 2022-01-03T22:50:26.920Z },
      { date: 2022-01-03T22:32:29.660Z },
      { date: 2022-01-03T22:22:58.480Z }
    ]

And I'm trying to do the next:

    for (const prop in obj) {
        obj[prop].date = moment(obj[prop].date).tz("America/Vancouver").format("YYYY-MM-DD");
    }

Expecting:

    [
      { date: 2022-01-02 },
      { date: 2022-01-03 },
      { date: 2022-01-03 },
      { date: 2022-01-03 },
      { date: 2022-01-03 }
    ]

But I can't

let obj = [
{ date: "2022-01-03T05:00:41.560Z" },
{ date: "2022-01-03T22:54:33.980Z" },
{ date: "2022-01-03T22:50:26.920Z" },
{ date: "2022-01-03T22:32:29.660Z" },
{ date: "2022-01-03T22:22:58.480Z" }
];
for (const prop in obj) {
  obj[prop].date = moment(obj[prop].date).tz("America/Vancouver").format("YYYY-MM-DD");
}
console.log(obj)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment-with-locales.min.js" integrity="sha512-42PE0rd+wZ2hNXftlM78BSehIGzezNeQuzihiBCvUEB3CVxHvsShF86wBWwQORNxNINlBPuq7rG4WWhNiTVHFg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

modify it, I keep getting the original array

5
  • 2
    Your data is an array of objects so you probably want for...of instead: for (const obj of data) {...}. Commented Jul 7, 2022 at 14:13
  • 1
    The code,as is in your example, seems to work fine (*see jsfiddle.net/cdysmvf3). Perhaps your data is coming from an immutable source and so you cannot modify them. Where do you get the obj from ? Commented Jul 7, 2022 at 14:26
  • 1
    Please update the snippet to a minimal reproducible example - I could not find the correct moment from cdn to help you Commented Jul 7, 2022 at 14:33
  • @Gabriele Petrioli I'm getting it from a db.collection.find().then(()=>{}) Commented Jul 7, 2022 at 14:47
  • I wonder if the problem is due to moment-timezone installation not having the Vancouver data. With plain old moment, and .utcOffset('-7:00'), (and a little syntax cleanup) I see your code working just fine. Commented Jul 7, 2022 at 15:01

2 Answers 2

3

I recommend to use Array.map for this transformation:

const data = [
  { date: '2022-01-03T05:00:41.560Z' },
  { date: '2022-01-03T22:54:33.980Z' },
  { date: '2022-01-03T22:50:26.920Z' },
  { date: '2022-01-03T22:32:29.660Z' },
  { date: '2022-01-03T22:22:58.480Z' }
];

const res = data.map(
  ({ date }) => ({
    date: moment(date).tz("America/Vancouver").format("YYYY-MM-DD")
  })
);

console.log(res);

in this example we are deconstructing each object and only extracting date, and return a new object that's only got date in it

Taking below comments into account, try Array.forEach:

data.forEach(
  ({ date }, index) => {
    data[index].date = moment(date).tz("America/Vancouver").format("YYYY-MM-DD");
  }
);
Sign up to request clarification or add additional context in comments.

1 Comment

This doesn't modify data which the OP seems to want
1

For what it's worth, here's the OP snippet running, improved slightly by using for-of, and (not sure if this is an improvement or a work-around) by using the native timezone offset.

let data = [
  { date: "2022-01-03T05:00:41.560Z" },
  { date: "2022-01-03T22:54:33.980Z" },
  { date: "2022-01-03T22:50:26.920Z" },
  { date: "2022-01-03T22:32:29.660Z" },
  { date: "2022-01-03T22:22:58.480Z" }
];

for (const obj of data) {
  obj.date = moment(obj.date).utcOffset('-7:00').format("YYYY-MM-DD");
}
console.log(data);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.