-2

I am working on a chat application and I have an array of messages which each have a timestamp property. I need to get all the days in the array where at least 1 message is sent, like in WhatsApp. The idea is to display a separator between the messages on different days, with the separator being just before the first message of the day specified.

For example if the array of messages was

[
 {
  message: "Hey",
  timestamp: (1st January 2020)
 },
 {
  message: "Hi",
  timestamp: (1st January 2020)
 },
 {
  message: "Hello",
  timestamp: (3rd January 2020)
 },
]

Then the expected output would be

[(1st January 2020), (3rd January 2020)]

or even better

[
 {
  type: "Seperator",
  day: (1st January 2020)
 },
 {
  message: "Hey",
  timestamp: (1st January 2020)
 },
 {
  message: "Hi",
  timestamp: (1st January 2020)
 },
 {
  type: "Seperator",
  day: (3rd January 2020)
 },
 {
  message: "Hello",
  timestamp: (3rd January 2020)
 }
]
Benjamin Sommer
  • 1,058
  • 3
  • 15
  • 35

1 Answers1

1

All you need to do is check if the current time has been visited. If it hasn't, add a separator.

let timestamps = [
 {
  message: "Hey",
  timestamp: '1st January 2020'
 },
 {
  message: "Hi",
  timestamp: '1st January 2020'
 },
 {
  message: "Hello",
  timestamp: '3rd January 2020'
 },
];

const separateTimestamps = ary => {
  let splitTimes = new Array();
  let timestamp;
  for (let i=0; i<ary.length; i++) {
    if (timestamp != ary[i].timestamp) {
      timestamp = ary[i].timestamp;
      splitTimes.push({
        type: 'Seperator',
        timestamp: timestamp
      });
    }

    splitTimes.push(ary[i]);
  }

  return splitTimes;
}


console.log(separateTimestamps(timestamps));
Joseph Cho
  • 4,033
  • 4
  • 26
  • 33
  • Thanks for the reply, unfortunately this code adds a separator between each message. Could you please check the function? Thanks – Benjamin Sommer Mar 11 '20 at 22:01
  • The code adds a separator between each new timestamp. Try running it in chrome console. The timestamp was converted to a string for simplicity. – Joseph Cho Mar 11 '20 at 22:02
  • Oh, I see how it works now. How could I adapt this so that I can input a timestamp in seconds? Would I need to use something like `new Date(seconds).getDay()` ? – Benjamin Sommer Mar 11 '20 at 22:06
  • 1
    I got it working by converting the seconds input to a short date format like `30/12/20` – Benjamin Sommer Mar 11 '20 at 22:10
  • Close, but that wouldn't work if your timestamps span several months (e.g. 1/9 and 2/9). You would need to write a separate function that would extract `yyyy/mm/dd` from your timestamp. – Joseph Cho Mar 11 '20 at 22:11
  • Nice job. `dd/mm/yy` format works, anything without time you get the idea. – Joseph Cho Mar 11 '20 at 22:11
  • Thanks, I used the prototype function found here in case you are interested: https://stackoverflow.com/a/2035706/9244664 – Benjamin Sommer Mar 11 '20 at 22:12
  • That works. If your program becomes more robust spend an hour or so studying `moment.js`. They have a much cleaner api for this sort conversion. It will help you deal with external factors like time zones, daylight savings, etc. – Joseph Cho Mar 11 '20 at 22:14
  • I have used Moment js in the past but I didn't find any way it could help in this situation. – Benjamin Sommer Mar 11 '20 at 22:16