admin管理员组

文章数量:1197761

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.

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.

Share Improve this question edited Apr 10, 2013 at 7:10 Ben 52.9k36 gold badges132 silver badges153 bronze badges asked Apr 8, 2013 at 12:10 MonicaMonica 6154 gold badges12 silver badges31 bronze badges 2
  • re: duplicate. doesn't seem to be a dupe to me (though related is different). – Michael Durrant Commented Apr 8, 2013 at 12:15
  • Where does this date? It is a string or a date object? – Shankar Cabus Commented Apr 8, 2013 at 12:32
Add a comment  | 

2 Answers 2

Reset to default 22

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";

you can use .replace

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

DEMO

本文标签: javascriptSplit time into hoursminutes and secondsStack Overflow