简体   繁体   中英

Moment Timezone not returning expected result

I might be doing something silly here. But essentially, the time in Lisbon right now is 12:27 PM

but the following returns 14:27 (EU central time)

  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, format, timeZone); 
  return time.isBetween(startMoment, endMoment); 

I tried several combinations and I get the wrong answer everytime. For example if I set timeZone to be "Europe/Warsaw" it returns 15:27 whereas it should be 13:27.

EDIT: const currentTime = moment().tz("Europe/London").format() returns the correct time for London. However, the return statement moment(currentTime).isBetween(startMoment, endMoment) still reads "moment(correntTime)" as the local time.

isBetween return boolean . And isBetween runs on date object. You are trying to run on time zone object. which is different from date object

 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM