最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why moment is adding one hour? - Stack Overflow

programmeradmin0浏览0评论

When I use:

console.log(moment(1000*60*60*1).format('hh[h] mm[min] ss[sec]'));

I get 2h 0min 0sec instead of just 1hour.
I made a workaround by adding .subtract(1, 'hour') like this:

console.log(moment(1000*60*60*1).subtract(1, 'hour').format('hh[h] mm[min] ss[sec]'));

I'm still learning this library that i found today. Am I missing something?
What am I supposed to do if I have milliseconds and I want to get a formatted date out of it?

When I use:

console.log(moment(1000*60*60*1).format('hh[h] mm[min] ss[sec]'));

I get 2h 0min 0sec instead of just 1hour.
I made a workaround by adding .subtract(1, 'hour') like this:

console.log(moment(1000*60*60*1).subtract(1, 'hour').format('hh[h] mm[min] ss[sec]'));

I'm still learning this library that i found today. Am I missing something?
What am I supposed to do if I have milliseconds and I want to get a formatted date out of it?

Share Improve this question edited Mar 30, 2017 at 12:02 Mattia Pettenuzzo asked Mar 30, 2017 at 10:47 Mattia PettenuzzoMattia Pettenuzzo 1951 silver badge15 bronze badges 6
  • 3 are you in a timezone that is UTC+1 perhaps – Jaromanda X Commented Mar 30, 2017 at 10:50
  • Yes, but it does matter? – Mattia Pettenuzzo Commented Mar 30, 2017 at 10:51
  • for me your code returns 06h 30min 00sec – Pranav Patel Commented Mar 30, 2017 at 10:51
  • 1 so, the +1 hour of your timezone isn't a clue as to what is going on? ... – Jaromanda X Commented Mar 30, 2017 at 10:51
  • I've tried and it's the timezone that matters – Mattia Pettenuzzo Commented Mar 30, 2017 at 10:53
 |  Show 1 more ment

1 Answer 1

Reset to default 8

Moment counts your timezone, to get the time without timezone offset you can use

moment(1000*60*60*1).utc().format('hh[h] mm[min] ss[sec]')

Or

moment.utc(1000*60*60*1).format('hh[h] mm[min] ss[sec]')
发布评论

评论列表(0)

  1. 暂无评论