hello guys i would like to get difference date using javascript, in php Im using this script to get difference date :
$tgl1 = date('Y-m-d');
$tgl2 = '2019-12-31';
$pecah1 = explode("-", $tgl1);
$date1 = $pecah1[2];
$month1 = $pecah1[1];
$year1 = $pecah1[0];
$pecah2 = explode("-", $tgl2);
$date2 = $pecah2[2];
$month2 = $pecah2[1];
$year2 = $pecah2[0];
$jd1 = GregorianToJD($month1, $date1, $year1);
$jd2 = GregorianToJD($month2, $date2, $year2);
$selisih = $jd2 - $jd1;
and how to do in javascript
This may help :)
date1 = "2018-12-25"; //date1
date2 = "2019-01-01"; //date2
var one_day = 1000 * 60 * 60 * 24;
var x = date1.split("-");
var y = date2.split("-");
var new_date1 = new Date(x[0], (x[1] - 1), x[2]);
var new_date2 = new Date(y[0], (y[1] - 1), y[2]);
var month1 = x[1] - 1;
var month2 = y[1] - 1;
_Diff = Math.ceil((new_date2.getTime() - new_date1.getTime()) / (one_day));
console.log(_Diff);
Related
Here I'm finding time difference between two dates and number of hours get calculated like this.
var startdate = $filter('date')(addTeamtask.startdate, 'yyyy-MM-dd hh:mm:ss');
var enddate = $filter('date')(addTeamtask.duedate, 'yyyy-MM-dd 23:59:59');
var sYear = startdate.slice(0, 4);
var eYear = enddate.slice(0, 4);
if (sYear > eYear) { var year = sYear - eYear; var yearhour = year * 8640; } else { year = eYear - sYear; yearhour = year * 8640; }
var sMonth = startdate.slice(5, 7);
var eMonth = enddate.slice(5, 7);
if (sMonth > eMonth) { var month = sMonth - eMonth; var monthhour = month * 720; } else { month = eMonth - sMonth; monthhour = month * 720; }
var Sday = startdate.slice(8, 10);
var Eday = enddate.slice(8, 10);
if (Sday > Eday) { var day = Sday - Eday; var hours = day * 24; } else { day = Eday - Sday; hours = day * 24 }
var totalhours1 = yearhour + monthhour + hours;
var totalhours = totalhours1 +'59:59.544';
But while i send this hours on string format to my web API, it shows {00:00:00} i.e actually time is not binding, Where as if i manually insert hours like this.
var totalhours = "15:58:50.373"
On my API show time as what i had send {15:58:50.373}. So Somebody help me how i may send time dynamically.
I had fix my issue on my own way. like this
var startdate = $filter('date')(addTeamtask.startdate, 'yyyy-MM-dd hh:mm:ss');
var enddate = $filter('date')(addTeamtask.duedate, 'yyyy-MM-dd 23:59:59');
var sYear = startdate.slice(0, 4);
var eYear = enddate.slice(0, 4);
if (sYear > eYear) { var year = sYear - eYear; var yearhour = year * 8640; } else { year = eYear - sYear; yearhour = year * 8640; }
var sMonth = startdate.slice(5, 7);
var eMonth = enddate.slice(5, 7);
if (sMonth > eMonth) { var month = sMonth - eMonth; var monthhour = month * 720; } else { month = eMonth - sMonth; monthhour = month * 720; }
var Sday = startdate.slice(8, 10);
var Eday = enddate.slice(8, 10);
if (Sday > Eday) { var day = Sday - Eday; var hours = day * 24; } else { day = Eday - Sday; hours = day * 24 }
var totalhours1 = yearhour + monthhour + hours;
var totalhours = totalhours1 +'59:59.544';
var totalHoursCaluclutation=totalhours.tostring();
Now API accept my request... Thanks for your commands. Kindly keep support me.
Please i need some help in javascript or jQuery :
how to get current date and time to minutes?
i have a datetime in that format : 'd/m/Y H:m' (example 25/12/2016 22:35) in input . how to get it and convert it to minutes ? and make difference between it and current datetime
Thank you in advance for help. I tried all but nothing works
To get the current date and time to minutes, you could divide the milliseconds:
var theMinutes = new Date().getTime() / (1000 * 60);
To get the input and convert it to minutes, you could use a Date object, and divide the milliseconds:
var someDateTime = document.getElementById("someDateTime").value;
var someParts = someDateTime.split(" ");
var theDate = someParts[0];
var theTime = someParts[1];
var dateParts = theDate.split("/");
var timeParts = theTime.split(":");
var theDay = dateParts[0];
var theMonth = dateParts[1];
var theYear = dateParts[2];
var theHours = timeParts[0];
var theMinutes = timeParts[1];
var someDateTimeObject = new Date(theYear, theMonth, theDay, theHours, theMinutes, 0 );
var someDateTimeObjectMillis = someDateTimeObject.getTime();
var someDateTimeObjectMinutes = someDateTimeObjectMillis / (1000 * 60);
Hello thank you for all yours answers
Firsly i get the current time using php ( in Milliseconds) then convert pickup date to milliseconds and get differences between them in hours :
promo (in minutes) i get 767 hours normally i should get : 47 hours and some minutes
var startdate=document.getElementById('pickupdate').value; //03-11-2015 15:49
var someParts = startdate.split(" ");
var theDate = someParts[0];
var theTime = someParts[1];
var dateParts = theDate.split("-");
var timeParts = theTime.split(":");
var theDay = dateParts[0];
var theMonth = dateParts[1];
var theYear = dateParts[2];
var theHours = timeParts[0];
var theMinutes = timeParts[1];
var someDateTimeObject = new Date(theYear, theMonth, theDay, theHours, theMinutes, 0 );
var someDateTimeObjectMillis = someDateTimeObject.getTime();
//var someDateTimeObjectMinutes = someDateTimeObjectMillis / (1000 * 60);
alert(someDateTimeObjectMillis);
var maintenant = <?php echo(round(microtime(true) * 1000)); ?>;
alert(maintenant);
var diff = someDateTimeObjectMillis - maintenant;
alert(diff);
var promo = diff/(60 * 1000);
alert(promo); // 46079
thank you for your support
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;
I am getting the values from two text fields as date
var start_actual_time = $("#startPoint_complete_date").val();
var end_actual_time = $("#endPoint_complete_date").val();
which gives value
start_actual_time = 01/17/2012 11:20
end_actual_time = 01/18/2012 12:20
now i want to find out the duration in HH:MM format between these two dates (which is 25:00 in this case)
how can i do it...
var start_actual_time = "01/17/2012 11:20";
var end_actual_time = "01/18/2012 12:25";
start_actual_time = new Date(start_actual_time);
end_actual_time = new Date(end_actual_time);
var diff = end_actual_time - start_actual_time;
var diffSeconds = diff/1000;
var HH = Math.floor(diffSeconds/3600);
var MM = Math.floor(diffSeconds%3600)/60;
var formatted = ((HH < 10)?("0" + HH):HH) + ":" + ((MM < 10)?("0" + MM):MM)
alert(formatted);
See demo : http://jsfiddle.net/diode/nuv7t/5/ ( change mootools in jsfiddle
or open http://jsfiddle.net/nuv7t/564/ )
Working example:
gives alert message as 6:30
$(function(){
var startdate=new Date("01/17/2012 11:20");
var enddate=new Date("01/18/2012 12:20");
var diff = new Date(enddate - startdate);
alert(diff.getHours()+":"+diff.getMinutes());
});
First, I recommend this: http://www.mattkruse.com/javascript/date/ to convert the string to a date object, but you can convert it any way you want. Once converted, you can do this:
var difference_datetime = end_datetime.getTime() - start_datetime.getTime()
The getTime() function gets the number of milliseconds since 1970/01/01. Now you have the number of milliseconds of difference, and you can do whatever you want to convert to higher numbers (eg. divide by 1000 for seconds, divide that by 60 for minutes, divide that by 60 for hours, etc.)
I did something simular on a blog to see how long im together with my gf :P
http://daystogether.blogspot.com/ and this is how I did it:
// *****Set the unit values in milliseconds.*****
var msecPerMinute = 1000 * 60;
var msecPerHour = msecPerMinute * 60;
var msecPerDay = msecPerHour * 24;
// *****Setting dates*****
var today = new Date();
var startDate = new Date('10/27/2011 11:00:00');
// *****Calculate time elapsed, in MS*****
var interval = today.getTime() - startDate.getTime();
var days = Math.floor(interval / msecPerDay );
interval = interval - (days * msecPerDay );
var weeks = 0;
while(days >= 7)
{
days = days - 7;
weeks = weeks + 1;
}
var months = 0;
while(weeks >= 4)
{
weeks = weeks - 4;
months = months + 1;
}
// Calculate the hours, minutes, and seconds.
var hours = Math.floor(interval / msecPerHour );
interval = interval - (hours * msecPerHour );
var minutes = Math.floor(interval / msecPerMinute );
interval = interval - (minutes * msecPerMinute );
var seconds = Math.floor(interval / 1000 );
BTW this is just javascript, no jquery ;)
here you go:
function get_time_difference(start,end)
{
start = new Date(start);
end = new Date(end);
var diff = end.getTime() - start.getTime();
var time_difference = new Object();
time_difference.hours = Math.floor(diff/1000/60/60);
diff -= time_difference.hours*1000*60*60;
if(time_difference.hours < 10) time_difference.hours = "0" + time_difference.hours;
time_difference.minutes = Math.floor(diff/1000/60);
diff -= time_difference.minutes*1000*60;
if(time_difference.minutes < 10) time_difference.minutes = "0" + time_difference.minutes;
return time_difference;
}
var time_difference = get_time_difference("01/17/2012 11:20", "01/18/2012 12:20");
alert(time_difference.hours + ":" + time_difference.minutes);
start_date.setTime(Date.parse(start_actual_time));
end_date.setTime(Date.parse(end_actual_time));
//for check
start_date.toUTCString()
end_date.toUTCString()
/**
* Different in seconds
* #var diff int
*/
var diff = (end_date.getTime()-start_date.getTime())/1000;