将HH:MM:SS转换为momentjs中的秒数

时间:2022-11-13 02:49:40

I have a string with 12 hour 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做到这一点?

2 个解决方案

#1


27  

Try something like this:

尝试这样的事情:

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

returns 43812

返回43812

#2


1  

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()

#1


27  

Try something like this:

尝试这样的事情:

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

returns 43812

返回43812

#2


1  

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()