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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With