1

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".

Euph
  • 95
  • 2
  • 9

6 Answers6

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
2

Use slice

"15:50:30".slice(0,-3)
joopmicroop
  • 891
  • 5
  • 15
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