时区未返回预期结果

问题描述

我可能在这里做一些愚蠢的事情。但实际上,现在里斯本的时间是12:27 PM

但以下内容返回14:27(欧盟中心时间)

  const time = moment.tz("Europe/Lisbon")
  const timeZone = "Europe/Lisbon"
  const format = "'HH[:]mm'"
  const startMoment = moment.tz(item.startTime,format,timeZone); 
  const endMoment = moment(item.endTime,timeZone); 
  return time.isBetween(startMoment,endMoment); 

我尝试了几种组合,但每次都得到错误的答案。例如,如果我将timeZone设置为“欧洲/华沙”,它将返回15:27,而应该是13:27。

编辑:const currentTime = moment().tz("Europe/London").format()返回伦敦的正确时间。但是,返回语句moment(currentTime).isBetween(startMoment,endMoment)仍将“ moment(correntTime)”作为本地时间。

解决方法

isBetween返回布尔值。 isBetween在日期对象上运行。您正在尝试在time zone对象上运行。与日期对象不同

    const time = moment.tz("Europe/Lisbon")
    const timeZone = "Europe/Lisbon"
    const format = "'HH[:]mm'"
    const startMoment = moment().subtract(8,'months').tz(timeZone).format(); 
    const endMoment = moment(new Date()).tz(timeZone).format() ; 
    console.log("startMoment",startMoment)
    console.log("endMoment",endMoment)
    console.log(moment.tz(new Date(),"Europe/Lisbon").format())
    console.log(moment('2020-09-30').isBetween(startMoment,endMoment));
    <script src="https://momentjs.com/downloads/moment.min.js"></script>
    <script src="https://momentjs.com/downloads/moment-timezone-with-data.js"></script>

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...