Thiago
Thiago

Reputation: 1565

Converting to int a split string

Guys, Iknow this sounds ridiculous, but I'm only getting the rsult NaN of this...

tim="09:50"
time = tim.split(":");
minutes = parsefloat

How can I get a integer value of that?

Thks a lot!

Upvotes: 0

Views: 2479

Answers (4)

Andrew Whitaker
Andrew Whitaker

Reputation: 126072

var tim = "9:50";
var time = tim.split(":");
var minutes = parseInt(time[1], 10);

Upvotes: 4

Brad Christie
Brad Christie

Reputation: 101614

var time = "09:50";
var timeParts = time.split(':');
var hour = parseInt(timeParts[0],10), minute = parseInt(timeParts[1],10);
// you need to specify base because JS tries to make an assumption while parsing.

Should do the trick. You're on right track, but parseInt/parseFloat are functions that need arguments.

Upvotes: 2

jon_darkstar
jon_darkstar

Reputation: 16788

var minutes = parseInt(time[1]);
var hours = parseInt(time[0]);

Upvotes: 1

alexn
alexn

Reputation: 59012

This should do what you want:

var time = "09:50";
var parts = time.split(":");
var hours = parseInt(parts[0], 10);
var minutes = parseInt(parts[1], 10);

Upvotes: 4

Related Questions