0

I have a nodejs app. Below is the code to see online clients and the time. But It always shows wrong time and online clients are shown 4 hours ago always. What's wrong in this code?

type state = {
  activePopup: any
  popupData: any
  activeBuilder: boolean
  activeBroadcast: boolean
  broadcast: string
  loading: boolean
  error: boolean
  rows: any
  online: any
  offline: any
  totalUsers: any
  newIdentifier: any
}

function timePassed(timeInMs: any) {
  // const seconds = Math.floor(timeInMs / 1000)
  const minutes = Math.floor(timeInMs / (1000 * 60))
  const hours = Math.floor(timeInMs / (1000 * 60 * 60))
  const days = Math.floor(timeInMs / (1000 * 60 * 60 * 24))
  const weeks = Math.floor(timeInMs / (1000 * 60 * 60 * 24 * 7))
  if (weeks > 0) return weeks === 1 ? weeks + ' week ago' : weeks + ' weeks ago'
  else if (days > 0) return days === 1 ? days + ' day ago' : days + ' days ago'
  else if (hours > 0)
    return hours === 1 ? hours + ' hour ago' : hours + ' hours ago'
  else if (minutes > 0)
    return minutes === 1 ? minutes + ' minute ago' : minutes + ' minutes ago'
  else return 'Now'
}

Another piece of code which is responsible for time check is this...

function timePassed(timestamp: string) {
  const timeInMs = new Date().getTime() - new Date(timestamp).getTime()
  // const seconds = Math.floor(timeInMs / 1000)
  const minutes = Math.floor(timeInMs / (1000 * 60))
  const hours = Math.floor(timeInMs / (1000 * 60 * 60))
  const days = Math.floor(timeInMs / (1000 * 60 * 60 * 24))
  const weeks = Math.floor(timeInMs / (1000 * 60 * 60 * 24 * 7))
  if (weeks > 0) return weeks === 1 ? weeks + ' week ago' : weeks + ' weeks ago'
  else if (days > 0) return days === 1 ? days + ' day ago' : days + ' days ago'
  else if (hours > 0)
    return hours === 1 ? hours + ' hour ago' : hours + ' hours ago'
  else if (minutes > 0)
    return minutes === 1 ? minutes + ' minute ago' : minutes + ' minutes ago'
  else return 'Now'
}

0 Answers0