There is a line "09:15:00".
How to get values 09 and 15?
|
8 answers
The split(s) method allows you to turn a string into an array, breaking it by separator s .
for example
var names = 'Маша, Петя, Марина, Василий'; var arr = names.split(', '); for (var i = 0; i < arr.length; i++) { alert( 'Вам сообщение ' + arr[i] ); } Displays:
Вам сообщение Маша Вам сообщение Петя Вам сообщение Марина Вам сообщение Василий In your case, the separator will be a colon:
var d = "09:15:00"; var splitted = d.split(':'); document.write('Первое значение: ' + splitted[0] + ', Второе значение: ' + splitted[1]); Read more about the split method here.
|
2 solutions
var str = '09:15:00'; var arr = str.split(':'); var strSplitHours = arr[0]; var strSplitMinutes = arr[1]; document.write('вариант1 - Часы:'+strSplitHours+' Минуты:'+strSplitMinutes+'<br>'); var regexHours = str.replace(/(\d\d).*/,'$1'); var regexMinutes = str.replace(/\d\d.(\d\d).*/,'$1'); document.write('вариант2 - Часы:'+regexHours+' Минуты:'+regexMinutes); |
I still figured out how to show off and use jQuery :)
Well, along the way applied ES6.
In 49m chrome works.
for (let str of ["09:15:00", "09:15:02", "00:15:00", "09:00:47"]) { const date = $("<input type=time step=1>").val(str)[0].valueAsDate; $("body").append($("<p>").text( "{{getUTCHours| h}} {{getUTCMinutes| min}} {{getUTCSeconds| sec}}" .replace(/\{\{(\w+)\|(.*?)\}\}/g, (s, m, t) => (m=date[m]()) ? m+t : "") )); } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> - then replace
varwithlet.) - betonimig - @betonimig, also replaced with const) - Qwertiy ♦
- in
for, you can alsolettheconstchange) - betonimig - @betonimig, different more interesting. - Qwertiy ♦
|
And so here you can simply do not bother at all =)
var foo = "09:15:00"; document.write('часы: ' + foo[0]+foo[1] + ' ; минуты: ' + foo[3]+foo[4]) |
Another option with match
var str = '09:15:00'; var names = ["Часы", "Минуты", "Секунды"]; document.write('<pre>' + JSON.stringify(str.match(/\d+/g).map(function(el, i) { return { [names[i]]: el }; }), null, 2) + '</pre>'); document.write('<pre>' + JSON.stringify(str.match(/\d+/g).reduce(function(acc, el, i) { acc[names[i]] = el; return acc; }, {}), null, 2) + '</pre>'); document.write('<pre>' + JSON.stringify(str.match(/\d+/g).reduce(function(acc, el, i) { return Object.assign(acc, { [names[i]]: el }); }, {}), null, 2) + '</pre>'); - Why not in one object? Let's reduce we will finish? - Qwertiy ♦
- @Qwertiy, why? :) these are different elements :) - Grundy
- @Qwertiy, added a variant with reduce :-) - Grundy
- By the way, you also have ES6 due to computed properties :) - Qwertiy ♦
- @Qwertiy, only in map - in version - good old ES5 :) - Grundy
|
Normal match for example
var s = '09:15:00'; var r = s.match(/(\d\d):(\d\d):(\d\d)/); var h = r[1]; var m = r[2]; var s = r[3]; console.log(h, m, s); |
I’ll add a variant with ES6 to my piggy bank (almost never works without transpiling):
let [hours, minutes] = "09:15:00".split(':').map(Number) .map(Number) can be added if you want to get exactly the numeric value. If you need a line - remove.
|
The problem is solved by standard JS methods:
var d = new Date("July 21, 1983 09:15:00"); d.getMinutes(); d.getHours(); console.log(d.getMinutes(), d.getHours()); - Not any line can be passed to the constructor - Grundy
|
splitfor such cases - Alexey Shimansky