Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/447.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 时间函数在nodejs中工作不正常_Javascript_Node.js_Nodejs Server - Fatal编程技术网

Javascript 时间函数在nodejs中工作不正常

Javascript 时间函数在nodejs中工作不正常,javascript,node.js,nodejs-server,Javascript,Node.js,Nodejs Server,我有一个nodejs应用程序。下面是查看在线客户端的代码和时间。但它总是显示错误的时间和在线客户端显示4小时前总是。这个代码有什么问题 type state = { activePopup: any popupData: any activeBuilder: boolean activeBroadcast: boolean broadcast: string loading: boolean error: boolean rows: any online: a

我有一个nodejs应用程序。下面是查看在线客户端的代码和时间。但它总是显示错误的时间和在线客户端显示4小时前总是。这个代码有什么问题

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'
}
另一段负责时间检查的代码是

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'
}

检查时区
timeInMs
请检查编辑。在第二个代码中,有一个与服务器时间的检查。const timeInMs=new Date().getTime()-new Date(timestamp).getTime()第二个工作正常。你现在面临的问题是什么?我总是让时间倒退4小时。我不知道为什么会这样……服务器所在的时区和时间戳也应该属于同一时区