简体   繁体   English

将时间分为小时,分钟和秒

[英]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 . 例如,如果时间是09:11:21。 I need it as 09 hrs, 11 min and 21 seconds. 我需要它在09小时,11分钟和21秒。

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. 之后我想在新的texbox中得到结果。结果是通过打卡时间和休息时间之间的差异来计算的。

For example if the punch-in time is 09:00:00 and the breaktime is 01:10:00. 例如,如果打卡时间是09:00:00,休息时间是01:10:00。 I need the result as 07 hrs, 50 min and 00 seconds. 我需要结果为07小时,50分钟和00秒。

You can use split() 你可以使用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 正如@TheJohlin所说, 编辑它更好地使用parseInt而不是$ .trim

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 你可以使用.replace

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

DEMO DEMO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM