How to calculate the different between two datepickers
Clash Royale CLAN TAG#URR8PPP
How to calculate the different between two datepickers
im trying to calculate the date different for a two datepicker in JavaScript. I tried following other posts, but everytime i calculate the date diff * price, but i'm not getting a result. Can someone please direct me into the right direction in order to accomplish my goal in finding date differential between two datepickers.
//input/saving data for the next form
$(".next").click(function(){
start = $('#checkin').val();
ends = $('#checkout').val();
diff = (ends - start);
days = diff/1000/60/60/24;
//roomPrice = price inserted into selection type(Single = 65, Double = 100, Suite = 120)
roomPrice = Rprices[selectType.value];
//preparing form5
fields = [$('#FirstName').val() + ' ' + $('#LastName').val(),
$('#phone').val(),
$('#email').val(),
$('#StreetAddress').val(),
$('#City').val(),
$('#state').val(),
$('#zip').val(),
$('#checkin').val(),
$('#checkout').val(),
$('#RoomType').val(),
$('#NumOfPeople').val(),
document.getElementById("total").value = days * roomPrice];
Are you using the jQuery Datepicker widget?
– kRiZ
Dec 9 '14 at 22:33
Sorry for the late reply. Yes, i'm using the JQuery Datepicker widget.
– Abner
Dec 10 '14 at 18:34
2 Answers
2
I'm not sure what you are asking but:
start = $('#checkin').val(); // assuming this is a date
ends = $('#checkout').val(); // assuming this is a date
var start_date = new Date(start); // assuming the format is correct
var end_date = new Date(ends); // assuming the format is correct
var diff_ms = end_date.getTime() - start_date.getTime(); // assuming both dates exist
will get you the time difference in milliseconds.
Thank you!! That solved my problem. All i did was change end_date.getTime() to end_date.getDate() - .... since i wanted the difference in days not in milliseconds. Thank you, you made my day.
– Abner
Dec 11 '14 at 6:02
You're welcome :)
– sebnukem
Dec 11 '14 at 15:27
getDateDiff(time1, time2)
var t2 = new Date(time1);
var t1 = new Date(time2);
var diffMS = t1 - t2;
var ret = "";
ret = ret + parseInt(diffMS / 1000 / 60 / 60).toString() + " hours ";
diffMS = diffMS - parseInt(diffMS / 1000 / 60 / 60) * 60 * 60 * 1000;
ret = ret + parseInt(diffMS / 1000 / 60).toString() + " min ";
diffMS = diffMS - parseInt(diffMS / 1000 / 60) * 60 * 1000;
ret = ret + parseInt(diffMS / 1000).toString() + " sec";
return ret ;
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You should cut the code down to just the relative parts
– MiiinimalLogic
Dec 9 '14 at 21:41