0

I need to get subtract the timezone offset from the current time using momentjs

Suppose I have below offsets

1) +05:30 2) -05:00 3) -06:30

Now I need to subtract this time from the current time

moment(). // what should be here?

Thank you!!!

Profer
  • 553
  • 8
  • 40
  • 81
  • RAJESH Not working with my nodejs. `const date = moment.utc().utcOffset("-04:00")` – Profer May 08 '19 at 11:14
  • @Luke - no, you don't add or subtract a time zone offset. You apply it. – Matt Johnson-Pint May 08 '19 at 17:50
  • @Profer - you'll have to do better than "not working". What doesn't work? That code looks fine to me. – Matt Johnson-Pint May 08 '19 at 17:50
  • @MattJohnson `const date = moment.utc().utcOffset("-04:00")` It does not add offset to the current time. neither add nor subtract – Profer May 09 '19 at 07:26
  • @MattJohnson Actually I use `.toDate()` as well. Something like `const date = moment.utc().utcOffset("-04:00").toDate()` – Profer May 09 '19 at 07:59
  • `Date` objects only track a specific timestamp - a unique moment in time. Adding or subtracting would give a different moment in time. Thus rightfully, adjusting the offset or time zone of how one views that moment should not change the moment or the underlying timestamp itself. See also: https://dilbert.com/strip/1998-03-14 – Matt Johnson-Pint May 09 '19 at 18:23
  • To be more direct - don't call `.toDate()`. Instead use `.format()` to produce the desired string. – Matt Johnson-Pint May 09 '19 at 18:31
  • 1
    That question is about adding a duration of time. 5 hours and 30 minutes after the current time. It isn't the same as applying a time zone offset. In other words, just because India's local time is 5 hours and 30 minutes ahead of UTC, doesn't mean that events occur 5 hours and 30 minutes later in real-time. It just means the local clocks are offset by that amount. – Matt Johnson-Pint May 10 '19 at 15:54
  • 1
    Put another way: If I were to set my clock to India's local time, and you were to set your clock to UTC, both clocks would be correct for their purpose. If I were to then send you a message over the internet, you would not have to wait 5 hours and 30 minutes to receive it. You'd still receive it almost instantly. – Matt Johnson-Pint May 10 '19 at 15:57

0 Answers0