Monica
Monica

Reputation: 615

Split time into hours, minutes and seconds

I have to split time into hours, minutes and seconds. For example if the time is 09:11:21 . I need it as 09 hrs, 11 min and 21 seconds.

I have two textboxes to enter the punch-in time and break time. After that I want to get the result in a new texbox.The result is calculated by the difference between punchin time and break time.

For example if the punch-in time is 09:00:00 and the breaktime is 01:10:00. I need the result as 07 hrs, 50 min and 00 seconds.

Upvotes: 9

Views: 22634

Answers (2)

Adil
Adil

Reputation: 148180

You can use split()

Live Demo

arr = strDate.split(':')
hour = $.trim(arr[0]) + " hrs";
min = $.trim(arr[1]) + " min";
sec = $.trim(arr[2]) + " seconds";

Edit its better to use parseInt instead of $.trim as @TheJohlin told

Live Demo

strDate = "12 : 40 :12";
arr = strDate.split(':');
hour = parseInt(arr[0]) + " hrs";
min = parseInt(arr[1]) + " min";
sec = parseInt(arr[2]) + " seconds";

Upvotes: 22

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67217

you can use .replace

var xDate="12:35:45";
alert(xDate.replace(":"," hrs, ").replace(":"," Min and ") + " secs.");

DEMO

Upvotes: 0

Related Questions