简体   繁体   English

如何使用 moment.js 将小时和分钟转换为分钟?

[英]How to convert hours and minutes to minutes with moment.js?

I need to convert hours and minutes in minutes values.我需要将小时和分钟转换为分钟值。 With pure JavaScript Date object I do the following:使用纯 JavaScript 日期对象,我执行以下操作:

var d = new Date();
var minutes = d.getHours() * 60 + d.getMinutes();

I've just switched to moment.js and looking for better solution likes the following:我刚刚切换到moment.js并寻找更好的解决方案,如下所示:

var minutes = moment(new Date()).toMinutes()

Is there is something like this?有这样的吗?

I think your best bet is to create a Duration and then get the minutes using asMinutes . 我认为最好的办法是创建一个Duration ,然后使用asMinutes获取分钟。 This is probably clearer when describing an interval of time. 在描述时间间隔时,这可能更清楚。

moment.duration().asMinutes()

Here is the reference in the docs. 这是文档中的参考

You could use: 你可以使用:

var m = moment(new Date());
var minutes = (m.hour()*60) + m.minute();

http://momentjs.com/docs/#/get-set/minute/ http://momentjs.com/docs/#/get-set/minute/

对于那些希望从现有Moment对象获取分钟数的人,您可以先将其格式化为您需要的任何内容,然后使用duration时间以分钟为单位获取持续时间:

moment.duration(myMomentObjectToConvert.format("HH:mm")).asMinutes()
const moment = require('moment')
var date = new Date()
console.log(date) // 2018-11-09T10:01:33.131Z

// 1.
console.log( 'object', moment(new Date()).toObject() )
// { years: 2018,
// months: 10,
// date: 9,
// hours: 13,
// minutes: 1,
// seconds: 33,
// milliseconds: 135 }

// 2.
console.log( 'array', moment(new Date()).toArray() ) // array [ 2018, 10, 9, 13, 1, 33, 141 ]

// 3.
console.log( 'year', moment(new Date()).toObject().years ) // year 2018
console.log( 'day of month', moment(new Date()).toObject().date ) // day of month 9
console.log( 'hours', moment(new Date()).toObject().hours ) // hours 13
console.log( 'minutes', moment(new Date()).toObject().minutes ) // minutes 1
console.log( 'seconds', moment(new Date()).toObject().seconds ) // seconds 33
console.log( 'milliseconds', moment(new Date()).toObject().milliseconds ) // milliseconds 139

// 4.
console.log( 'weeks', moment(new Date()).weeks() ) // weeks 45
console.log( 'day of week', moment(new Date()).days() ) // day of week 5
console.log( 'day of month', moment(new Date()).date() ) // day of month 9
console.log( 'hours', moment(new Date()).hours() ) // hours 13
console.log( 'minutes', moment(new Date()).minutes() ) // minutes 1
console.log( 'seconds', moment(new Date()).seconds() ) // seconds 33
console.log( 'milliseconds', moment(new Date()).milliseconds() ) // milliseconds 141

I didn't needed moment.js, at least worked for my scenarios: 我不需要moment.js,至少适用于我的场景:

 console.log('converting 2 hours, 20 minutes and 120 seconds to minutes'); var hours = 2, minutes = 20, seconds = 120, timeInminutes = (hours * 60) + minutes + (seconds / 60); console.log('2 hours, 20 minutes and 120 seconds to minutes is', timeInminutes, ' minutes') 

只是为了简化 - '07:07:00' 是 HH:mm:ss 格式,这将在几分钟内转换

moment.duration('07:07:00').asMinutes()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM