Javascript 在 momentjs 中将 HH:MM:SS 转换为秒

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29745586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:54:25  来源:igfitidea点击:

Convert HH:MM:SS to seconds in momentjs

javascriptjqueryangularjsmomentjs

提问by Ramesh Rajendran

I have a string with 12hour formatted time only

我有一个12只有小时格式的时间字符串

var time="12:10:12: PM"

var time="12:10:12: PM"

I want to convert this string to seconds.

我想将此字符串转换为秒。

how can i do this in moment.js?

我怎样才能做到这一点moment.js

回答by Travis Collins

Try something like this:

尝试这样的事情:

moment('12:10:12: PM', 'HH:mm:ss: A').diff(moment().startOf('day'), 'seconds');

returns 43812

返回 43812

回答by pkerckhove

I was just trying to achieve the same thing. using https://momentjs.com/

我只是想达到同样的目的。使用 https://momentjs.com/

This is how I got what I wanted (03:11:23 => 11483 seconds)

这就是我得到我想要的(03:11:23 => 11483 秒)

myVar = moment.duration(myVar).asSeconds()