-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,092
  • 5
  • 15
  • 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
  • 959,406
  • 173
  • 1,780
  • 1,769
0
var splits = str.split(":");
if(splits.length == 3) {
    //is valid
    var hour = splits[0];
    var min = splits[1];
    var sec = splits[2];
}
merv
  • 53,208
  • 11
  • 148
  • 196
silly
  • 7,565
  • 2
  • 23
  • 36