Moment Timezone not returning expected result

196 views Asked by At

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.

1

There are 1 answers

5
Shubham Verma On

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>