简体   繁体   中英

minus date and time by javascript

I have two times like : "03/03/2011 07:41:00 pm" and "04/03/2011 08:41:00 pm"

I want to make minus between it it's will be:

"03/03/2011 07:41:00 pm" - "04/03/2011 08:41:00 pm" = h:m:s

like this website

http://www.cobone.com/deals/dubai?lang=en

If you can convert the string into a Javascript Date object, you can do something like this:

//this time is 6 hours, 45 minutes, 30 seconds
var date0 = new Date(24330000);
var date1 = new Date(0);
var millisDifference = date0.getTime() - date1.getTime();
//this will alert "6:45:30"
alert(Math.floor(millisDifference / 3600000) + ":" + Math.floor((millisDifference % 3600000) / 60000) + ":" + Math.floor((millisDifference % 60000) / 1000));

Ideally though for parsing Strings to Dates and vice versa you want to be using a library like, for example, this .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM