大佬教程收集整理的这篇文章主要介绍了在javascript中将军事时间转换为标准时间的最佳方法,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
$scope.convertTimeAMPM = function(timE){ //var time = "12:23:39"; var time = time.split(':'); var hours = time[0]; var minutes = time[1]; var seconds = time[2]; $scope.timeValue = "" + ((hours >12) ? hours -12 :hours); $scope.timeValue += (minutes < 10) ? ":0" : ":" + minutes; $scope.timeValue += (seconds < 10) ? ":0" : ":" + seconds; $scope.timeValue += (hours >= 12) ? " P.m." : " A.m."; //console.log( timevalue); }
但是当我运行程序时,我对输出节目不满意. .
样本输出:
20:00:00 8:0:0 P.m. 08:00:00 08:0:0 A.M 16:00:00 4:30:0 P.m.
我想实现如下所示的输出:
20:00:00 8:00:00 P.m. 08:00:00 8:00:00 A.M 16:30:00 4:30:00 P.m.
那里有什么建议吗?谢谢
var time = "16:30:00"; // your input time = time.split(':'); // convert to array // fetch var hours = number(time[0]); var minutes = number(time[1]); var seconds = number(time[2]); // calculate var timeValue; if (hours > 0 && hours <= 12) { timeValue= "" + hours; } else if (hours > 12) { timeValue= "" + (hours - 12); } else if (hours == 0) { timeValue= "12"; } timeValue += (minutes < 10) ? ":0" + minutes : ":" + minutes; // get minutes timeValue += (seconds < 10) ? ":0" + seconds : ":" + seconds; // get seconds timeValue += (hours >= 12) ? " P.m." : " A.m."; // get AM/PM // show alert(timevalue); console.log(timevalue);
以上是大佬教程为你收集整理的在javascript中将军事时间转换为标准时间的最佳方法全部内容,希望文章能够帮你解决在javascript中将军事时间转换为标准时间的最佳方法所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。