-1

I'm building a chat app so when a user sends message, a time stamp is also sent in database. I followed this answer for this implementation.

database()
        .ref('/')
        .child(`chats/${merged_uid}/${+new Date()}`)
        .set({
          message: msgInput,
          number: current_user.phoneNumber,
          uid: current_user.id,
          time: +new Date(),
        });

and i'm rendering this time stamp in my app in a flatlist in following way

<Text style={styles.timeStamp}>
                {new Date(item.time * 1000).getHours() +
                  ':' +
                  new Date(item.time * 1000).getMinutes()}
              </Text>

but my timestamp is not rendering correctly. These are the problems i'm facing with timestamp.

  1. It's not correct. i.e my current time of message is 4:44pm so it shows 16:50, here hours is correct but mins aren't. and sometimes it just doesn't give time correct. in next following 6 message time was 13:31, 14:35, 16:13, 19:1, 17:44, 20:44 respectively while current time was 4:44pm.
  2. Time is not formatted. i.e How can i convert it to 12hrs time format
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Aqsa Maan
  • 75
  • 1
  • 10
  • 1
    Maybe it is better also to explain what is this *1000 that you are trying to do, Date param can be value, but if you multiply by 1000 for sure it will change – Marcel Kohls Apr 07 '21 at 12:12
  • I was following this [answer](https://stackoverflow.com/questions/847185/convert-a-unix-timestamp-to-time-in-javascript) – Aqsa Maan Apr 07 '21 at 12:16
  • in your first code you did not divide Date() by 1000, why would you multiply it by 1000 in second block of codes? We can follow answers but we need to follow it correctly, and logically. This is probably nothing to do with Dates any more, but how we use the read and understand example codes. – Someone Special Apr 07 '21 at 12:17
  • 1
    So, this is the right documentation for the Date class: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date. I recommend to take a look there to compare. – Marcel Kohls Apr 07 '21 at 12:18

2 Answers2

1

The issue there is that you are multiplying by 1000, changing the correct value given by +new Date().

Your code on the browser console for 14:25:

(new Date(+new Date() * 1000)).getMinutes()
6
(new Date(+new Date() * 1000)).getMinutes()
35
(new Date(+new Date() * 1000)).getMinutes()
49
(new Date(+new Date() * 1000)).getMinutes()
0

The code without the multiplyer for 14:27:

(new Date(+new Date())).getMinutes()
27
(new Date(+new Date())).getMinutes()
27
(new Date(+new Date())).getMinutes()
27
Marcel Kohls
  • 1,650
  • 16
  • 24
1

Below is the JS function to get 12 hr formatted timestamp from JS Date. It is actually unsure from provided details, what kind of data is in item.time.

function getTimeStamp(date) {
    var mn = "AM",
        h = date.getHours(),
        m = date.getMinutes(),
        s = date.getSeconds(),
        ms = date.getMilliseconds();
    if (h >= 12) {
        mn = "PM";
        if (h > 12) {
            h -= 12;
        }
    }
    if (h == 0) {
        h = 12;
    }
    return `${String(h).padStart(2, 0)}:${String(m).padStart(2, 0)}:${String(s).padStart(2, 0)}.${String(ms).padStart(3, 0)} ${mn}`;
}

// Check
var hour = 0;
while (hour < 25) {
    var d = new Date();
    d.setHours(hour);
    console.log(getTimeStamp(d));
    hour++;
}
Wazeed
  • 1,230
  • 1
  • 8
  • 9