I have a string of time in the format "HH:MM:SS". I want only "HH:MM". How can I do? For example I have "15:50:30". And I want to obtain with javascript "15:50".
Asked
Active
Viewed 9,898 times
1
-
Would using `Substring` be feasible. – pinegulf Dec 20 '17 at 08:58
6 Answers
3
USe substring
var date='09:57:22';
date = date.substring(0,5);

Federico klez Culloca
- 26,308
- 17
- 56
- 95

Stéphane Ammar
- 1,454
- 10
- 17
-
@joopmicroop please don't add your two cents to an answer. That's what comments are for. – Federico klez Culloca Dec 20 '17 at 09:05
1
function addZero(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
var d = new Date();
var h = addZero(d.getHours());
var m = addZero(d.getMinutes());
console.log(h + ":" + m )

vicky patel
- 699
- 2
- 8
- 14
0
check this link date to minutes and hours
var dateWithouthSecond = new Date();
dateWithouthSecond.toLocaleTimeString(navigator.language, {hour: '2-digit', minute:'2-digit'});

Fadi Abo Msalam
- 6,739
- 2
- 20
- 25
0
you can easily pass the time into get date format. example.
time ="2:33:58 PM"; your time
pass it to the new date variable.
time = new date (time);
then new time will have the entire date .
then make a string in that get the time and hours . time = time.getHours+":"+time.getMinutes

imdisney
- 109
- 1
- 13
0
Many of these slice
and substring
answers gets the job done, but i would like to point to Regular Expressions as they can be more flexible in regards to inputs.
For instance would this Regular Expression handle both single and double digits, which would throw slice
and substring
off:
var tests = ['12:12:12', '1:1:1', '1:12:1', '12:12:1212: 12:12'];
for (var _i = 0, tests_1 = tests; _i < tests_1.length; _i++) {
var test = tests_1[_i];
console.log("result of regex on \"" + test + "\" is: \"" + /\d+:\d+/ig.exec(test).shift() + "\"");
}

Emil S. Jørgensen
- 6,216
- 1
- 15
- 28