如何从 Javascript 中的 HH:MM AM 时间字符串中减去小时数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6365706/
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
How can I subtract hours from a HH:MM AM time string in Javascript?
提问by Danny Garcia
What's the best way to subtract a few hours from a time string formatted as such:
从格式如下的时间字符串中减去几个小时的最佳方法是什么:
8:32 AM
I thought about splitting the string at the colon but when subtracting 3 hours from, say, 1:00 AM I get -2:00 AM instead of the desired 10:00 PM.
我想过在冒号处拆分字符串,但是当从凌晨 1:00 减去 3 小时时,我得到 -2:00 AM 而不是所需的 10:00 PM。
回答by Marc B
Most reliable method is to convert it into a JS date object, then do you math on that
最可靠的方法是将其转换为 JS 日期对象,然后您对其进行数学运算
var olddate = new Date(2011, 6, 15, 8, 32, 0, 0); // create a date of Jun 15/2011, 8:32:00am
var subbed = new Date(olddate - 3*60*60*1000); // subtract 3 hours
var newtime = subbed.getHours() + ':' + subbed.getMinutes();
the Date object accepts either year/month/day/hour/minute/second/milliseconds ORa unix-style timestamp of milliseconds-since-Jan-1-1970 for the constructor.
Date 对象接受构造函数的年/月/日/小时/分钟/秒/毫秒或Unix 样式的毫秒时间戳,自 1970 年 1 月 1 日以来。
回答by forivall
回答by Liangliang Zheng
If you just want to play with only the hours, I think the following would be easy for you:
如果你只想玩几个小时,我认为以下对你来说很容易:
var timeStr = '1:30 PM'
var parts = timeStr.split(':');
var hour = parseInt($.trim(parts[0]));
hour -= 3;
if(hour <= 0){
// easily flip it by adding 12
hour += 12;
// swap am & pm
if(parts[1].match(/(AM|am)/)){
parts[1] = parts[1].replace('AM', 'PM').replace('am', 'pm')
// keep the case
} else {
parts[1] = parts[1].replace('PM', 'AM').replace('pm', 'am')
}
}
// final answer
timeStr = hour + ':' + parts[1];
回答by Tamer
you can use that peace of code :
你可以使用代码的和平:
var T1=new Date("September 5, 2005 8:10:00");
var T2=new Date("September 5, 2005 13:35:00");
var diff=new Date();
diff.setTime(T2-T1);
alert(diff.getHours()+":"+diff.getMinutes())
hope this helps..
希望这可以帮助..
回答by Pete - MSFT
Hardest part is parsing in your time - I've just added Jan 1, 2009 to the start of the parse method so that it parses it nicely and you don't need to write your own. (not that it's difficult). Collapse the code below into a few lines - expanded to show the steps.
最难的部分是在您的时间进行解析 - 我刚刚将 2009 年 1 月 1 日添加到 parse 方法的开头,以便它可以很好地解析它并且您不需要编写自己的。(并不是说这很难)。将下面的代码折叠成几行 - 展开以显示步骤。
<html>
<head>
<script type="text/javascript">
function calculateTime(stringTime) {
var hoursToSubtract = 1;
var oldTime = Date.parse('Jan 1, 2009 ' + stringTime);
var newTime = new Date(oldTime - 1000 * 60 * 60 * hoursToSubtract);
var hours = newTime.getHours();
var minutes = newTime.getMinutes();
var designation = "PM";
if ((hours == 0 || hours == 24) && minutes == 0)
designation = 'MIDNIGHT';
else if (hours == 12 && minutes == 0)
designation = 'NOON'
else if (hours < 12)
designation = 'AM';
else
hours -= 12;
document.write('new time = ' + hours + ':' + minutes + ' ' + designation );
}
</script>
</head>
<body>
<script type="text/javascript">
calculateTime('8:30 AM');
calculateTime('8:30 PM');
calculateTime('12:10 PM');
</script>
</body>
</html>
回答by RobG
Lots of answers on the right track, one more to consider:
很多答案都在正确的轨道上,还有一个需要考虑:
// Assumes timeString is hh:mm am/pm
function addHours(timeString, h) {
var t = timeString.match(/\d+/g);
var am = /am$/.test(timeString);
var d = new Date();
d.setHours(+t[0] + (am? 0 : 12) + +h, +t[1]);
return formatTime(d.getHours() + ':' + d.getMinutes());
}
function formatTime(t) {
function addZ(n) {
return n<10? '0'+n : ''+n;
}
var t = t.split(':');
var m = (t[0] > 12)? 'pm' : 'am';
return addZ(t[0]%12 || t[0]) + ':' + addZ(t[1]) + ' ' + m;
}
alert( addHours('12:15 am', -13) ); // 11:15 pm
回答by Zack
You might try a jQuery library like DateJs. Below are some examples shown from that link:
您可以尝试使用像DateJs这样的 jQuery 库。以下是该链接中显示的一些示例:
// Number fun
(3).days().ago();
// Convert text into Date
Date.parse('today');
Date.parse('t + 5 d'); // today + 5 days
Date.parse('next thursday');
Date.parse('February 20th 1973');
Date.parse('Thu, 1 July 2004 22:30:00');