0

I'm not sure how to spit out the right date/time using moment.js base on diff timezone offset

I've tried USA : MA & TX

console.log(moment().utcOffset('-5').format('ddd MM/DD/y H:mm:ss')); //MA
console.log(moment().utcOffset('-6').format('ddd MM/DD/y H:mm:ss')); //TX

I kept getting

Fri 09/17/2021 18:28:21 //MA
Fri 09/17/2021 18:28:21 //TX

I supposed to get

Fri 09/17/2021 18:28:21 //MA
Fri 09/17/2021 17:28:21 //TX

Any hints for me ?

code-8
  • 54,650
  • 106
  • 352
  • 604
  • 1
    I don't personally use moment.js, but from a quick look at the docs, could it be that you are passing "-5" as a string instead of a number? So .utcOffset(-5) instead of .utcOffset('-5')? – Thomas Bay Sep 17 '21 at 22:40
  • See also https://stackoverflow.com/a/32893771/65694 – Alex Nolasco Sep 17 '21 at 22:41

1 Answers1

1

Try this

console.log(moment.utc().format('ddd MM/DD/y H:mm:ss'), 'UTC');
console.log(moment.utc().add(-5, 'hours').format('ddd MM/DD/y H:mm:ss'), 'MA');
console.log(moment.utc().add(-6, 'hours').format('ddd MM/DD/y H:mm:ss'), 'TX');
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
  • While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Adrian Mole Sep 18 '21 at 00:38