How to get number of weeks from two dates in jQuery 1.0?
I have tried this but no luck:
//start and end date is in format '10-0ct-2015'
var end = $("span[id$=spEstDeliveryDate] input[type=text]").val();
var start = $("span[id$=spPODeliveryDate] input[type=text]").val();
var f = $.datepicker.parseDate("dd-M-yy", end);
var date1 = new Date(f);
date1 = (date1.getDate() + '/' + (date1.getMonth() + 1 ) + '/' + date1.getFullYear());
f = $.datepicker.parseDate("dd-M-yy", start);
var date2 = new Date(f);
date2 = (date2.getDate() + '/' + (date2.getMonth() + 1) + '/' + date2.getFullYear());
var totalWeeks = Math.floor((date1 - date2 + 1) / (1000 * 60 * 60 * 24) / 7);
You convert the dates to strings before trying to do the math on them. Leave them as Date objects:
var f = $.datepicker.parseDate("dd-M-yy", end);
var date1 = new Date(f);
f = $.datepicker.parseDate("dd-M-yy", start);
var date2 = new Date(f);
var totalWeeks = Math.floor((date1 - date2) / (1000 * 60 * 60 * 24) / 7);
Related
I have a date like this 2017-07-25 09:30:49, when I subtract 2017-07-25 10:30:00 and 2017-07-25 09:30:00, I need a result like 1 Hours.
I can't find correct search key for googling what I need.
Anyone know what should I search on google ? or someone knows some function about that?
PS. Mysql or Javascript
Try with date object in javascript
Like this
var d1 = new Date("2017-07-25 10:30:00");
var d2 = new Date("2017-07-25 09:30:49")
var diff = Math.abs(d1-d2); // difference in milliseconds
Then convert the milliseconds to hours
var hours = parseInt((diff/(1000*60*60))%24);
You can go through it
Get the time difference between two datetimes
But the query is not clear do you want only the hour difference or you want the difference converted to hour format
Like what it will give if 2017-07-25 09:30:49 and 2017-07-26 10:30:00 ? 25 hour or 1 hour?
here a code example of how to do it
var date1 = new Date("2017-07-25 09:30:49");
var date2 = new Date("2017-07-25 10:30:00");
var datesum = new Date(date1 - date2);
var hours = datesum.getHours();
var minutes = datesum.getMinutes();
var seconds = datesum.getSeconds();
console.log(hours + " hour, " + minutes + " minutes, " + seconds + " seconds" )
var dateString = "2017-07-25 09:30:49";
var dateString2= "2017-07-25 11:30:00";
var reggie = /(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/;
var dateArray = reggie.exec(dateString);
var dateArray2= reggie.exec(dateString2);
var dateObject1= new Date(
(+dateArray[1]),
(+dateArray[2])-1, // Careful, month starts at 0!
(+dateArray[3]),
(+dateArray[4]),
(+dateArray[5]),
(+dateArray[6])
);
var dateObject2= new Date(
(+dateArray2[1]),
(+dateArray2[2])-1, // Careful, month starts at 0!
(+dateArray2[3]),
(+dateArray2[4]),
(+dateArray2[5]),
(+dateArray2[6])
);
var diff = Math.abs(dateObject2-dateObject1); // difference in milliseconds
var hours = parseInt((diff/(1000*60*60))%24);
Try with the below dateFormatter function :
var d1 = new Date("2017-07-25 10:30:00");
var d2 = new Date("2017-07-25 09:30:00")
var diff = Math.abs(d1-d2);
var d = dateFormatter(diff);
console.log(d);
function dateFormatter(t){
var cd = 24 * 60 * 60 * 1000;
var ch = 60 * 60 * 1000;
var cm = 60*1000;
var d = Math.floor(t / cd);
var h = '0' + Math.floor( (t - d * cd) / ch);
var m = '0' + Math.round( (t - d * cd - h * ch) / 60000);
var s = '0' + Math.round((t - (d * cd) - (h * ch) - (m * cm))/1000);
return d + " days, " + h.substr(-2) + " hours, " + m.substr(-2) + " minutes, " +s.substr(-2)+ " seconds";
}
I've created a booking calculator by date using JavaScript.
Basically I want the base price to be £25 for 1 day (24 hours) or less, and £10 for each additional day (each additional 24 hours).
Below is the main part of the code.
jQuery(document).ready(function($) {
var prequote=25.00;
var taxa = 10.00;
var hoje = new Date();
hoje=hoje.getTime();
i = 0;
$('#quote').click(function(event) {
var d1= $('#d1').val();
var d2= $('#d2').val();
var t1= $('#t1').val();
var t2= $('#t2').val();
console.log(d1);
console.log(d2);
console.log(t1);
console.log(t2);
// end - start returns difference in milliseconds
var date2 = new Date(d2);
var date1 = new Date(d1);
var millisecondsPerDay = 1000 * 60 * 60 * 24;
var millisBetween = date2.getTime() - date1.getTime();
// get days
var d = millisBetween / millisecondsPerDay;
//alert ('value of days is:' +d);
//alert ( new Date("1970-1-1 " + t2) - new Date("1970-1-1 " + t1) ) / 1000 / 60 / 60;
var h= ( new Date("1970-1-1 " + t2) - new Date("1970-1-1 " + t1) ) / 1000 / 60 / 60;
//alert ('value of hours is:' +h);
t1 =t1.split(':');
t2 =t2.split(':');
var dat1 = d1.split("-");
var dd1 = dat1[2];
var mm1 = dat1[1];
var yy1 = dat1[0];
var hh1 = t1[0];
var ms1 = t1[1];
var dat2 = d2.split("-");
var dd2 = dat2[2];
var mm2 = dat2[1];
var yy2 = dat2[0];
var hh2 =t2[0];
var ms2 = t2[1];
var x1 = yy1 + ',' + mm1 + ',' + dd1 + ' ' + hh1 + ':' + ms1;
var x2 = yy2 + ',' + mm2 + ',' + dd2 + ' ' + hh2 + ':' + ms2;
var ent = dd1 + '/'+ mm1 +'/'+yy1+' '+ hh1 + ':' + ms1;
var ext = dd2 + '/'+ mm2 +'/'+yy2+' '+ hh2 + ':' + ms2;
var xi = yy1 + ',' + mm1 + ',' + dd1 ;
var xj = yy2 + ',' + mm2 + ',' + dd2 ;
var start =new Date(x1);
//var start_i =new Date(xi);
var end = new Date(x2);
// var end_i = new Date(xj);
start = start.getTime();
end= end.getTime();
if(start === end){
alert('Min rental days is 1');
}
else if(start < end){
// hh1 = parseInt(hh1);ms1 = parseInt(ms1);hh2 = parseInt(hh2);ms2 = parseInt(ms2);;
/*while(start_i < end_i){
i++;
var newDate = start_i.setDate(start_i.getDate() + 1);
start_i = new Date(newDate);
}*/
i=d;
if(i >= 1 ){
if(h > 0 ){
i=i+1;
}
prequote = prequote + (taxa * (i-2));
prequote = parseFloat(prequote.toFixed(2));
}
$('#en-tex').text(ent);
$('#ex-t').text(ext);
$('#prequote').html(prequote);
$('#modal-img').modal('show');
prequote=25.00;
$('#tupd').val(ent);
$('#tdod').val(ext);
}
else{
alert('Please fill in all the date and time fields.');
}
});
The 1st issue is, if I select for example Monday 21st at 9:00am to Tuesday 22nd at 9:00am it doesn't count the fee as for 24 hours. Only if the end date is AFTER 9:00am.
Likewise for longer dates, it only charges for a day AFTER 24 hours and not from 24 hours on the dot.
2nd issue is, if somebody selects less than 24 hours (i.e Monday 21st at 9:00am to Tuesday 22nd at 7:00am) it minuses the £10 from £25. I want it to still quote the base price of £25.
I advise you to use momentJS. Is pretty easy to use.
var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b, 'days') // 1
Or just like that:
var a = moment('2016-06-06T21:03:55');//now
var b = moment('2016-05-06T20:03:55');
console.log(a.diff(b, 'minutes')) // 44700
console.log(a.diff(b, 'hours')) // 745
console.log(a.diff(b, 'days')) // 31
console.log(a.diff(b, 'weeks')) // 4
Edit.:
Adding some ideas. You can just simply use the example of #jeff:
dt1 = new Date('2016-01-21 20:00:00');
dt2 = new Date('2016-01-24 09:00:00');
dif = dt2-dt1;
dif = dif / ( 1000 * 60 * 60 * 24 );
days = Math.ceil(dif);
var total = 0;
if (days > 0) {
total = 25 + ( (days-1) * 10 ) // total = <first day with prequote 25 and then others days are 10 bucks>
}
console.log(total); // total of amout to be paid
Maybe thia can solve your issue.
Since you are working with whole days you can simplify the process by:
dt1 = new Date('2016-01-21 09:00:00');
dt2 = new Date('2016-01-22 09:00:00');
dif = dt2-dt1;
dif = dif / ( 1000 * 60 * 60 * 24 );
days = Math.ceil(dif);
The days value contains a whole number for the amount of days between the two dates. Do your pricing from this value.
When some one rent room for less than 24 hour. Value of i is become negative. From your expression
prequote = prequote + (taxa * (i-2));
If i is smaller than 2. taxa variable become negative and you have get price less than 25. Please use this below line
i=(i<2)?i=2:i;
From above expression. Your value cannot be smaller than £25. Hope it helps you
I want to format date in ISO YYYY-MM-DDThh:mm:ss.sTZD format using javascript.
I can convert current date string in yyyy-MM-dd'T'HH:mm:ss.SSSZ format. For example 2016-01-11T02:40:33.117Z. But I want to get like 2016-01-11T02:40:33.117+1100.
Is it possible in javascript?
Try the following.
var date = new Date(Date.now());
date.setTime(date.getTime() - (date.getTimezoneOffset() * 60000));
var output = date.toISOString().substring(0, date.toISOString().length - 1) + ((date.getTimezoneOffset() / 60) < 0 ? "-" : "+") + ((Math.abs(date.getTimezoneOffset() / 60) < 10) ? ("0" + Math.abs(date.getTimezoneOffset() / 60)) : test) + "00";
The output variable should be close to what you are looking for. (the + and - may be inversed)
NOTE: There may exist a better solution; I will post it if I find it.
function getCurrentDateFormated() {
var date = new Date();
date.setTime(date.getTime() - (date.getTimezoneOffset() * 60000));
var timeZoneHours = date.getTimezoneOffset() / 60;
var finalHours = Math.abs(timeZoneHours) < 10 ?
timeZoneHours < 0 ? '-0' + timeZoneHours.toString().substring(1)
: '+0' + timeZoneHours.toString().substring(1)
: timeZoneHours;
var timeZoneMin = ((timeZoneHours - Math.floor(timeZoneHours)) * 60) < 10 ?
'0' + (timeZoneHours - Math.floor(timeZoneHours)) * 60 : (timeZoneHours - Math.floor(timeZoneHours)) * 60;
var timeZone = finalHours + timeZoneMin;
return date.toISOString().substring(0, 20) + date.getMilliseconds() + timeZone;
}
Hi I am trying to get the difference between two dates but I keep on getting 0 as the answer anyone knows why please help. My code is like this
var d1 = "2015-04-30";
var d2 = "2015-04-14";
var startDay = new Date(d1);
var endDay = new Date(d2);
var millisecondsPerDay = 1000 * 60 * 60 * 24;
var millisBetween = startDay.getTime() - endDay.getTime();
var days = millisBetween / millisecondsPerDay;
//If I use df1 and df2 I am getting 0
var jsondate1 = new Date(getData.startDate).toISOString();
var jsondate2 = new Date(getData.startDate).toISOString();
var date = new Date(jsondate1);
var dates = new Date(jsondate2);
var df1 = date.getFullYear() + '-' + (date.getMonth()+1) + '-'
+date.getDate() ;
var df2 = dates.getFullYear() + '-' +(dates.getMonth()+1)
+ '-' +dates.getDate() ;
//If I am using the below two lines the answer is 16 but if I am using the above d1 and d2 the answer is zero
var d1 = "2015-4-30";// the date here is made up by console.debug(df1 )
var d2 = "2015-4-14"; // the date here is made up by console.debug(df2 )
I don't know where am I doing wrong
See the snippet - It shows 16.
var d1 = "2015-04-30";
var d2 = "2015-04-14";
var startDay = new Date(d1);
var endDay = new Date(d2);
var millisecondsPerDay = 1000 * 60 * 60 * 24;
var millisBetween = startDay.getTime() - endDay.getTime();
var days = millisBetween / millisecondsPerDay;
alert(days);
I think you should check your code at your end , because it's working as you want but some code syntax mistake will be there in your code. (may be)
It should work as is but you can try subtracting the dates without calling getTime():
var d1 = "2015-04-30";
var d2 = "2015-04-14";
var startDay = new Date(d1);
var endDay = new Date(d2);
var millisecondsPerDay = 1000 * 60 * 60 * 24;
var millisBetween = startDay - endDay;
var days = millisBetween / millisecondsPerDay;
alert(days);
After question update:
Both jsondate1 & jsondate2 are being created using the same date (getData.startDate), hence the difference is 0:
var jsondate1 = new Date(getData.startDate).toISOString();
var jsondate2 = new Date(getData.startDate).toISOString();
If you want date difference only. Then easiest way to do is.
var d1 = "2015-04-30";
var d2 = "2015-04-14";
Math.floor(( Date.parse(d1) - Date.parse(d2) ) / 86400000);
I have written a code to calculate the age of content on a website. It's inefficient, and the calculations are way off. I've written this script before, and last time it worked perfectly, but I can't find the damn file it's in.
I think the calculation problem is caused by the year. Can anyone suggest a fix up for me? Created date format is YYYYMMDD and output is in whole weeks (this is important), i.e. the example below should output '52' weeks.
var created='20120223';
var year=Number(created.substr(0,4));
var month=Number(created.substr(4,2))-1;
var day=Number(created.substr(6,2));
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1;
var curr_year = d.getFullYear();
var input_age = ((((curr_year - year)*31536000) + ((curr_month - month)*2678400) + ((curr_date - day)*86400))/604800).toFixed(0);
document.getElementById('item12345_input').value = input_age + ' weeks';
you are subtracting a month first. then you are again adding a month. try this
var created='20120223';
var year=Number(created.substr(0,4));
var month=Number(created.substr(4,2))-1;
var day=Number(created.substr(6,2));
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth();
var curr_year = d.getFullYear();
var input_age = ((((curr_year - year)*31536000) + ((curr_month - month)*2678400) + ((curr_date - day)*86400))/604800).toFixed(0);
alert(input_age + ' weeks');
Note Months are 0 based
<script>
var created ="20120223";
var yyyy = +created.substring(0, 4);
var mm = created.substring(4, 6)-1;
var dd = +created.substring(6, 8);
var createdDate = new Date(yyyy,mm,dd);
var ageMillis = new Date().getTime() - createdDate.getTime();
var MS_PER_WEEK = 1000/* ms /sec */
* 60 /* sec/min */
* 60 /* min/hr */
* 24 /* hr /day */
* 7 /* day/wk */;
var ageWeeks = parseInt(ageMillis / MS_PER_WEEK);
alert("Created on " +mm+"/"+dd+"/"+yyyy+" which is "+ageWeeks+ " week"+(ageWeeks==1?"":"s")+" ago");
</script>
you could try this:
var created='20120223';
var year=Number(created.substr(0,4));
var month = Number(created.substr(4,2));
if (Number(created.substr(4,2)) < 10){
month = '0'+ Number(created.substr(4,2));
}
var day=Number(created.substr(6,2));
var dt = year+'-'+month+'-'+day;
var dif = new Date().getTime() - Date.parse(dt);
var divWeek = 7 * 24 * 60 * 60 * 1000;
alert(Math.round(dif/divWeek));
Just subtract the 2 dates.
var createdDate = new Date(
+created.substring(0, 4), // Four digit year
created.substring(4, 6)-1, // Base-zero month
+created.substring(6, 8)); // Day of month
var ageMillis = (new Date) - createdDate;
var MS_PER_WEEK = 1000/* ms /sec */
* 60 /* sec/min */
* 60 /* min/hr */
* 24 /* hr /day */
* 7 /* day/wk */;
var ageWeeks = ageMillis / MS_PER_WEEK;