javascript 将时间分成小时、分钟和秒

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15878705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 02:26:20  来源:igfitidea点击:

Split time into hours, minutes and seconds

javascriptjqueryhtml

提问by Monica

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.

我必须将时间分成小时、分钟和秒。例如,如果时间是 09:11:21 。我需要它作为 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. I need the result as 07 hrs, 50 min and 00 seconds.

例如,打卡时间为 09:00:00,休息时间为 01:10:00。我需要结果为 07 小时 50 分 00 秒。

回答by Adil

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

Editits better to use parseIntinstead of $.trim as @TheJohlin told

parseInt正如@TheJohlin 所说,编辑它最好使用而不是 $.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";

回答by Rajaprabhu Aravindasamy

you can use .replace

您可以使用 .replace

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

DEMO

演示