使用Javascript:分割时间 [英] Javascript: split time
本文介绍了使用Javascript:分割时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图格式化包含时间数据,犹如以下 0时38分51秒
或 0点○○分59秒的变量值
或一时25分59秒
我需要格式化它像 25m59s
或相应小时 1h25m59s
0m59s
我做的一些分裂阅读和我已经开了一个头,但我越来越糊涂。
语法
斯普利特(前pression [,分隔符[,计数[对比]]])
块引用>时间=○时38分51秒
timeArray = time.split(:, - 1)
的document.write(timeArray [0]);更新
我创建了自己的功能,我不知道这是做它也是一个好办法;
函数formatTime(一){
无功时间= a.split(:);
无功小时= parseInt函数(时间[0],10);
变种分钟= parseInt函数(时间[1],10);
变种秒= parseInt函数(时间[2],10);
VAR X =的document.getElementById(时间);
x.innerHTML =小时+H+分钟+米+秒+的
}数值指明MyTime =○时38分51秒;
formatTime(数值指明MyTime);解决方案这应该这样做!
函数(时间){
VAR T = time.split(':'),
S = ['H','M','s'的]
I = 0;
对于(; I< t.length;我++){
T [i] = parseInt函数(T [I])== 0? ''形式:parseInt(T [I])+ S [I + s.length - t.length]
}
返回t.join('');
}<大骨节病> 小提琴 骨节病>
I am trying to format a variable value that contains time data and is like the following
00:38:51
or00:00:59
or01:25:59
I need to format it like
25m59s
or with hour accordingly1h25m59s
0m59s
I am doing some reading on splits and I have made a start but I am getting confused.
Syntax Split(expression[,delimiter[,count[,compare]]])
time = "00:38:51" timeArray = time.split(":",-1) document.write(timeArray[0]);
Update
I created my own function, I am not sure if this is a good way to do it also;
function formatTime(a) { var time = a.split(":"); var hours = parseInt(time[0], 10); var minutes = parseInt(time[1], 10); var seconds = parseInt(time[2], 10); var x = document.getElementById("time"); x.innerHTML = hours + "h" + minutes + "m" + seconds + "s" } myTime = "00:38:51"; formatTime(myTime);
解决方案This should do it!
function a(time) { var t = time.split(':'), s = ['h', 'm', 's'], i = 0; for(; i < t.length; i ++) { t[i] = parseInt(t[i]) == 0 ? '' : parseInt(t[i]) + s[i + s.length - t.length]; } return t.join(''); }
这篇关于使用Javascript:分割时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文