javascript - How to format number as time in Moment? - Stack Overflow

I am using bs-datepicker for save time as number in mongodb. In mongodb, it has time field with value i

I am using bs-datepicker for save time as number in mongodb. In mongodb, it has time field with value i.e, 38700000 and 41400000 which are 16.15 and 17.00 respectively.

Now, i want to format the above time in readable form. I am using moment 2.16. Here i try but unable to do jsfiddle

console.log(moment.unix(38700000).format('HH:mm')) //03:30, expect:16:15
console.log(moment.unix(41400000).format('HH:mm')) //09:30, expect:17:00

And, how to convert from HH:mm to number. e.g 18:71 convert into number

I am using bs-datepicker for save time as number in mongodb. In mongodb, it has time field with value i.e, 38700000 and 41400000 which are 16.15 and 17.00 respectively.

Now, i want to format the above time in readable form. I am using moment 2.16. Here i try but unable to do jsfiddle

console.log(moment.unix(38700000).format('HH:mm')) //03:30, expect:16:15
console.log(moment.unix(41400000).format('HH:mm')) //09:30, expect:17:00

And, how to convert from HH:mm to number. e.g 18:71 convert into number

Share Improve this question edited Feb 20, 2017 at 10:49 user7104874 asked Feb 20, 2017 at 10:36 user7104874user7104874 1,3813 gold badges16 silver badges21 bronze badges 4
  • 1 When I run console.log(moment(38700000).format('HH:mm')); & console.log(moment(41400000).format('HH:mm')); I get the values 10:45 and 11:30, respectively. These are both 5:30 mins away from your target values, so I would make sure your time zones are correct (probably best to use UTC+0). You can pass x as a format argument to get a unix timestamp from a date. – ZombieTfk Commented Feb 20, 2017 at 11:07
  • 1 I also want to point out that moment.unix(t) takes a unix timestamp in seconds, whereas moment(t) takes a unix timestamp in milliseconds. – ZombieTfk Commented Feb 20, 2017 at 11:21
  • @SamJudge, thanks for reply. My timezone is +5:30 GMT so, how to implement it? – user7104874 Commented Feb 20, 2017 at 11:44
  • 1 Simple, use .local() before .format() console.log(moment(38700000).local().format('HH:mm')) :) I see you're using mongo, so what is probably happening is you are uploading in your local time format, mongo stores it as UTC, and you are getting the UTC value back when you are expecting the local time. Doing the above should work. – ZombieTfk Commented Feb 20, 2017 at 12:24
Add a ment  | 

1 Answer 1

Reset to default 4

The data you have is in terms of milliseconds since 1970-01-01 00:00:00.000 UTC.

38700000 == 1970-01-01T10:45:00Z
41400000 == 1970-01-01T11:30:00Z

If you convert those to the time zone offset you mentioned in ments (UTC+05:30) then the time portion of the values line up to what you expected.

38700000 == 1970-01-01T10:45:00Z == 1970-01-01T16:15:00+05:30
41400000 == 1970-01-01T11:30:00Z == 1970-01-01T17:00:00+05:30

Your values don't align at all, because you are using moment's unix function, which expects time in seconds, not milliseconds (because Unix Time is in whole seconds unless otherwise specified).

Also, by relying on the local time zone, you may get different values than expected if the user is in a different time zone.

The correct way to get what you asked for is:

moment(38700000).utcOffset("+05:30").format("HH:mm") // "16:15"
moment(41400000).utcOffset("+05:30").format("HH:mm") // "17:00"

Do recognize though that this means your original data is not stored with reference to UTC, but with reference to UTC+05:30. It would be better if you had stored UTC-based values instead, so you would do:

moment.utc(58500000).format("HH:mm") // "16:15"
moment.utc(61200000).format("HH:mm") // "17:00"

Of course, the real problem here is that you're storing a time-of-day into a date-time field. Probably the best thing to do would to not use the Date type at all in your MongoDB data, but rather just store the string "16:15" or the equivalent number of total minutes as an integer (60 * 16 + 15 == 975).

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744837421a4596349.html

相关推荐

  • javascript - How to format number as time in Moment? - Stack Overflow

    I am using bs-datepicker for save time as number in mongodb. In mongodb, it has time field with value i

    2天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信