How can I subtract hours from a HH:MM AM time string in Javascript?
What's the best way to subtract a few hours from a time string formatted as such:
8:开发者_JAVA技巧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.
Most reliable method is to convert it into a JS date object, then do you math on that
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 OR a unix-style timestamp of milliseconds-since-Jan-1-1970 for the constructor.
Using moment.js
moment("8:32 AM", 'h:mm A').subtract('hours', 2).format('h:mm A')
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];
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..
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.
<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>
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
You might try a jQuery library like DateJs. Below are some examples shown from that link:
// 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');
精彩评论