0

I have an array of object like this:

const days = [
{
    _id: 12312323
    date : '30/12/2021'
    dateStatus : 'presence'
},
...
]

and I want to convert the date property from string to date object this way:

const convertToDateObject = (dateString: string): Date => {
    const dateParts: any[] = dateString.split("/");
    return new Date(+dateParts[2], dateParts[1] - 1, +dateParts[0]);
};

for (let i = 0; i < days.length; i++) {
    const d = days[i].date as string;
    days[i].date = convertToDateObject(d);
}

But this returns this converts the date value to this format 'Wed Dec 01 2021 00:00:00 GMT+0100 (Central European Standard Time)' instead of this format 2021-11-30T23:00:00.000Z**.

I am actually lost because when the given object does not have the _id property the formatting is working properly and it returns the wanted format, but when it does have the _id property then it returns me another format. Why?

2
  • 2
    If you're looking at the date in the console you will see the Date object\string as this: Wed Dec 01 2021 00:00:00 GMT+0100 (Central European Standard Time). But if you need its ISO representation in UTC, then use the following: new Date().toISOString() Commented Dec 30, 2021 at 9:53
  • @Max if I console.log the returned value from convertToDateObject(d) then it is good format, but the problem is happening when i am affecting this value to days[i].date = ... Commented Dec 30, 2021 at 9:56

1 Answer 1

4

But this returns this converts the date value to this format...

No, it doesn't. It gives you a Date object, which has no "format." You can get the date/time information from that object either in local time (apparently GMT+0100 where you are), or in UTC, depending on what methods you use. To get information from the object in local time, you use getHours, getMinutes, getSeconds (etc.), toString, toLocaleString, and similar. To get information from the object in UTC, you use getUTCHours, getUTCMinutes, getUTCSeconds (etc.), toUTCString, and similar.

The constructor you're using does work in local time. That is, it constructs a Date object for midnight (because you're not providing hours, minutes, seconds, or milliseconds) local time in your timezone (because you've used new Date). If you want to get midnight UTC, you can do new Date(Date.UTC(+dateParts[2], dateParts[1] - 1, +dateParts[0])); instead. The only difference is the moment in time that the Date object represents (the given date at midnight local time vs. the given date at midnight UTC).

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your answer bu still not getting what is happening, I don't think my question was clear. Actually the returned value from convertToDateObject(d) is working properly and it returns this format 2021-11-30T23:00:00.000Z but the problem is happening when i am putting this value in days[i].date it is being converted to a string '2021-11-30T23:00:00.000Z'. the buggy thing is when i delete the _id property from the object then everything works fine but it is not a good solution.
@SirageDb Where do see this incorrect format of the Date? Because there's a big chance, you have a local Date object in that variable, but the way this Date is displayed "seems" like it's a string.
@Max I have no idea why the returned value from convertToDateObject(d) is isoString but getting stored in days[i].date as a string. I found two solutions to the problem : 1. reconstructing each object day in the array days without the _id javascript days[i]={date : convertToDateObject(d), dateStatus: days[i].dateStatus} 2. keeping the _id and replacing the date using a spread operator like this (which is the solution i ended up with) : javascript days[i] = {...days[i], date : convertToDateObject(d)}
@SirageDb - "I have no idea why the returned value from convertToDateObject(d) is isoString..." It isn't, with the function you've shown in your question. Again: It's a Date object.

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.