Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert HH:MM:SS to seconds in momentjs

Tags:

I have a string with 12 hour formatted time only

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

I want to convert this string to seconds.

how can i do this in moment.js?

like image 314
Ramesh Rajendran Avatar asked Apr 20 '15 10:04

Ramesh Rajendran


People also ask

How do you convert HH MM SS to seconds?

To convert hh:mm:ss to seconds:Convert the hours to seconds, by multiplying by 60 twice. Convert the minutes to seconds by multiplying by 60 .

Is MomentJS deprecated?

Moment construction falls back to js Date. This is discouraged and will be removed in an upcoming major release. This deprecation warning is thrown when no known format is found for a date passed into the string constructor.

What is MomentJS used for?

MomentJS is a JavaScript library which helps is parsing, validating, manipulating and displaying date/time in JavaScript in a very easy way. This chapter will provide an overview of MomentJS and discusses its features in detail. Moment JS allows displaying of date as per localization and in human readable format.


2 Answers

Try something like this:

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

returns 43812

like image 72
Travis Collins Avatar answered Nov 10 '22 01:11

Travis Collins


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

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

myVar = moment.duration(myVar).asSeconds() 
like image 22
pkerckhove Avatar answered Nov 10 '22 00:11

pkerckhove