2

I need to sort an array in react native without affecting the timestamp. I'm getting details from the backend with timestamp and I want to sort it. I have done the sorting part with first come first basis using the following code.

 const members = out.game.gameMembers.map((elem) => {
      return {
        id: elem.userId._id,
        profileImage: elem.userId.profileImage,
        name: elem.userId.name,
        status: elem.status,
        position: elem.userId.position,
        isDeleted: elem.userId.isDeleted,
        createdAt: elem.createdAt,
        updatedAt:elem.updatedAt
      };
    });
    
    

    let sortedMembers = members.sort(function (x, y) {
      // return x.updatedAt - y.updatedAt;
     return new Date(x.updatedAt) - new Date(y.updatedAt);
    });

Here I'm getting the sorted members and sorting it with respect to their timestamp. But the problem is the time zone. If a user 'A' is from Dubai and another user 'B' is from India, the time difference is around two and half hours.

For example user from India updated their profile status at 11:30 AM and after 10 minutes user from Dubai updated his status where the time is 10:00 AM. So at time of sorting then User from Dubai comes first which can't be happen. How exactly solve this issue and sort the details using timestamp first come first in basis. Additionally I'm getting timezone of both Dubai user and Indian user. Thank you

2
  • 3
    Why don't you convert these timestamps to UTC and store them alongside data then sort them. Commented Dec 15, 2022 at 5:56
  • can you please explain with example. here I have data like updatedAt timestamp which looks like [{ name : "junaif", "updatedAt": "2022-12-15T05:27:33.152Z"},{ name : "junaif", "updatedAt": "2022-12-15T05:27:33.152Z"} ] and also user timezone and timeOffset. how can I perform this sorting. Commented Dec 15, 2022 at 6:22

1 Answer 1

0

Converting to UTC timezone should work:

const date = new Date()

// Date in the local time zone

console.log(date.toString())

// Date in UTC time zone

console.log(date.toUTCString())
Sign up to request clarification or add additional context in comments.

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.