[英]unable to get Am Pm from date object using javascript
我在javascript中有date对象,通过这个date对象,我试图获取AM
或PM
。 这是日期对象Mon May 01 2017 12:43:30 GMT+0530 (India Standard Time)
根据时间,它应该按照印度标准时间中午12:43返回pm
,但返回'am'
这是我的逻辑
var x = new Date("Mon May 01 2017 12:43:30 GMT+0530"); // (India Standard Time) var hours = x.getHours(); var hours = (hours + 24 - 2) % 24; var amPmStr = ' am'; if (hours == 0) { //At 00 hours we need to show 12 am hours = 12; } else if (hours > 12) { hours = hours % 12; amPmStr = ' pm'; } console.log(amPmStr,x)
看一看,希望这可以指导您正确的方向
我从var hours = (hours + 24 - 2) % 24;
删除了“ -2” var hours = (hours + 24 - 2) % 24;
既然没有意义。
var x = new Date("05 01 2017 12:43:30") var hours = x.getHours(); var minutes = x.getMinutes(); hours = (hours + 24) % 24; var amPmStr = ' am'; if (hours < 12) { //At 00 hours we need to show 12 am hours = 12; } else if (hours >= 12 && minutes > 00) { hours = hours % 12; amPmStr = ' pm'; } console.log(amPmStr)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.