javascript 在javascript中将军事时间转换为标准时间的最佳方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29206453/
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-28 10:13:38  来源:igfitidea点击:

Best way to convert military time to standard time in javascript

javascript

提问by Lee Lee

What is the best way to convert military time to am and pm time. . I have the following code and it works fine:

将军事时间转换为上午和下午时间的最佳方法是什么?. 我有以下代码,它工作正常:

$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);
}

But I am not satisfied in the output shows when I run my program. .

但是当我运行我的程序时,我对输出显示并不满意。.

Sample output:

示例输出:

20:00:00   8:0:0 P.M.
08:00:00   08:0:0 A.M
16:00:00   4:30:0 P.M.

I want to achieve the output which looks like the following:

我想实现如下所示的输出:

20:00:00   8:00:00 P.M.
08:00:00   8:00:00 A.M
16:30:00   4:30:00 P.M.

Is there any suggestions there? Thanks

有什么建议吗?谢谢

回答by Rahul Desai

You missed concatenating the string when minutes < 10and seconds < 10so you were not getting the desired result.

您错过了连接字符串的时间minutes < 10seconds < 10因此您没有获得所需的结果。

Convert string to number using Number()and use it appropriately as shown in the working code snippet below:

使用将字符串转换为数字Number()并适当地使用它,如下面的工作代码片段所示:

EDIT:Updated code to use Number()while declaration of hours, minutesand seconds.

编辑:更新代码以使用Number()while 声明hours,minutesseconds

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

Read up: Number()| MDN

阅读:Number()| MDN

回答by Huy Hoang Pham

As Nit recommend, Moment.js provides a simple solution to your problem.

正如 Nit 推荐的那样,Moment.js 为您的问题提供了一个简单的解决方案。

function convert(input) {
    return moment(input, 'HH:mm:ss').format('h:mm:ss A');
}

console.log(convert('20:00:00'));
console.log(convert('08:00:00'));
console.log(convert('16:30:00'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment.js"></script>

回答by Arlo

No need for a library

不需要图书馆

Here's a sweet & simple function I whipped up that should suit your needs exactly

这是我创建的一个甜蜜而简单的功能,它应该完全满足您的需求

function toStandardTime(militaryTime) {
    militaryTime = militaryTime.split(':');
    return (militaryTime[0].charAt(0) == 1 && militaryTime[0].charAt(1) > 2) ? (militaryTime[0] - 12) + ':' + militaryTime[1] + ':' + militaryTime[2] + ' P.M.' : militaryTime.join(':') + ' A.M.'
}

console.log(toStandardTime('16:30:00'));

And a more readable version in case it's needed

以及一个更易读的版本,以备不时之需

function toStandardTime(militaryTime) {
  militaryTime = militaryTime.split(':');
  if (militaryTime[0].charAt(0) == 1 && militaryTime[0].charAt(1) > 2) {
    return (militaryTime[0] - 12) + ':' + militaryTime[1] + ':' + militaryTime[2] + ' P.M.';
  } else {
    return militaryTime.join(':') + ' A.M.';
  }
}

console.log(toStandardTime('16:30:00'));