How to parse a string in jQuery

I would like to parse the following line which represents time (HH: MM: SS): 00:00:00

Does anyone know how I can get Hour, Minute or Seconds values?

Thank!

+3
source share
2 answers
var time = "00:00:00";
var parts = time.split(':');

alert("hours: " + parts[0] + ", minutes: " + parts[1] + ", seconds: ", + parts[2])
+10
source

I would probably go with a split (':') solution on my own, but here's an interesting alternative using native date parsing:

var time = '00:23:54';

var date = new Date('1/1/1900 ' + time);

// 0
date.getHours();

// 23
date.getMinutes();

// 54
date.getSeconds();
+4
source

Source: https://habr.com/ru/post/1750775/


All Articles