-5

Possible Duplicate:
how to split a string in javascript

Please help me to substring and separate the below string in three variables using javascript.

var str="HH:MM:SS";

variable 1 is HH variable 2 is MM variable 3 is SS

Thanks

Community
  • 1
  • 1
pal
  • 1,202
  • 6
  • 17
  • 27
  • And google next time! http://www.w3schools.com/jsref/jsref_substring.asp – Rene Pot Mar 01 '12 at 13:26
  • [split strings in javascript](http://stackoverflow.com/search?q=javascript+split+string+hours+minutes&submit=search) – Michael Berkowski Mar 01 '12 at 13:27
  • 1
    @Topener: A) Part of SO's goal is to be the top hit on Google searches, so "google next time" is not a useful suggestion. B) w3schools is a truly *rubbish* resource, recommend MDC or, of course, the specification (though the language is...obtuse). – T.J. Crowder Mar 01 '12 at 13:29

2 Answers2

2

The simplest way is split (specification | MDC):

var str = "HH:MM:SS";
var parts = str.split(":");
console.log(parts[0]); // "HH"
console.log(parts[1]); // "MM"
console.log(parts[2]); // "SS"
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0
var splits = str.split(":");
if(splits.length == 3) {
    //is valid
    var hour = splits[0];
    var min = splits[1];
    var sec = splits[2];
}
merv
  • 67,214
  • 13
  • 180
  • 245
silly
  • 7,789
  • 2
  • 24
  • 37