I need a javascript like this one
// Calculate days since 7st May 2016
var initialDate = new Date(2016, 5, 7);
var now = Date.now();
var difference = now - initialDate;
var millisecondsPerDay = 24 * 60 * 60 * 1000;
var daysSince = Math.floor(difference / millisecondsPerDay);
// Write result to HTML
document.getElementById('days_since').innerHTML = daysSince;
<div id="days_since"></div>
but I would like to display hours and my work is eg. monday-friday from 10am to 6pm and every saturday from 11am to 3pm.
Any advice?
Hi please refer this http://jsfiddle.net/yentup/fKA9X/1/
JS
$(function(){
var calcNewYear = setInterval(function(){
date_future = new Date(new Date().getFullYear() +1, 0, 1);
date_now = new Date();
seconds = Math.floor((date_future - (date_now))/1000);
minutes = Math.floor(seconds/60);
hours = Math.floor(minutes/60);
days = Math.floor(hours/24);
hours = hours-(days*24);
minutes = minutes-(days*24*60)-(hours*60);
seconds = seconds-(days*24*60*60)-(hours*60*60)-(minutes*60);
$("#time").text("Time until new year:\nDays: " + days + " Hours: " + hours + " Minutes: " + minutes + " Seconds: " + seconds);
},1000);
});
and HTML
<div id="time"></div>
Related
I want to check time difference between two times and get difference in minutes, using javascript and my time format is 12 hrs with am/pm
for example :
compare minutes difference between (10:35 am) -(01:15 pm)= ? minutes
But the problem is I am getting wrong values for this, so how to calculate minutes between two time using javascript
<script>
var timeStart = new Date("01/23/2020 " + "05:00 AM");
var timeEnd = new Date("01/23/2020 " + "06:30 PM");
var diff = (timeEnd - timeStart) / 60000;
var minutes = diff % 60;
var hours = (diff - minutes) / 60;
alert(minutes);
alert(hours);
</script>
This Could be the Short and Sweet solution
var diff = Math.abs(new Date('01/23/2020 06:30 PM') - new Date('01/23/2020 05:00 AM'));
var minutes = Math.floor((diff/1000)/60);
alert(minutes);
Total minutes will be calculated as
var timeStart = new Date("01/23/2020 " + "05:00 AM");
var timeEnd = new Date("01/23/2020 " + "06:30 PM");
var diff = (timeEnd - timeStart) / 60000;
var minutes = diff % 60;
var hours = (diff - minutes) / 60;
var totalMinutes = (hours*60)+minutes;
alert(totalMinutes);
this will give approximate minute diff in utc.
let startTime = new Date("01/23/2020 " + "05:00 AM");
let endTime = new Date("01/23/2020 " + "06:30 PM");
let diffInMinutes = (timeEnd - timeStart) / 60000;
let minutes = Math.floor(diff / 60);
to get the local time zone's minute difference you have to add the timezone offset when calculation the diff.
you can do so by ->
let diffInMinutesWithTZOffset = diff + new Date().getTimezoneOffset();
where diffInMinutesWithTZOffset will produce correct difference according to each timezone.
So I'm trying to build a very minimalistic countdown timer until a specific date (2015 July 31, 18:00:00 GMT) including total MS until the date, as well as a D:H:M:S:MS until the date.
What I got so far is at this codepen:
http://codepen.io/svbeon/pen/MwBJNJ
With this JS:
setInterval(times, 1);
function times() {
today = new Date();
July = new Date(Date.UTC(2015,06,31,18,0,0,0));
diffMs = ((July - today) - today.getTimezoneOffset() * 60000); // milliseconds between now & July 31
diffDays = (July.getDate() - today.getDate()); // days
diffHrs = (24 + ((July.getHours() - today.getHours()) + (today.getTimezoneOffset() / 60))); // hours
diffMins = (60 + (July.getMinutes() - today.getMinutes())); // minutes
diffSecs = (60 + (July.getSeconds() - today.getSeconds())); // seconds
diffMis = (1000 + (July.getMilliseconds() - today.getMilliseconds())); //milliseconds
if (diffMs <= 0) {
diffMs = 0;
diffDays = 0;
diffHrs = 0;
diffMins = 0;
diffSecs = 0;
diffMis = 0;
}
document.getElementById("timer").innerHTML = diffMs + " miliseconds";
document.getElementById("timer2").innerHTML = diffDays + " days " + diffHrs + " Hours " + diffMins + " Minutes " + diffSecs + " seconds " + diffMis + " milliseconds";
document.getElementById("date").innerHTML = "until " + July;
}
The problems I get here is that it can echo something like 14 days 24 hours 60 minutes 60 seconds 1000 milliseconds when it in fact should be 14 days 0 hours 0 minutes 0 seconds 0 milliseconds as well as after 18:00 UTC it still says 14 days instead of 13 days until midnight.
I got no real idea of how to simply fix these problems
Adding one less for example:
diffSecs = (59 + (July.getSeconds() - today.getSeconds())); // seconds
Will result in the timer showing 0 instead of 60, but will as well make the timer go 1 second wrong (minute or hour if you have it on their part)
So if you happen to have any idea how to fix this and help a not so skilled developer iIwould be really happy!!
I'd suggest using Moment.js, as it makes date/time manipulation much simpler and will probably help you solve this problem also.
For instance to get a difference between two dates in milliseconds:
var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b) // 86400000
Then create a duration from the result of the difference in milliseconds and use milliseconds(), seconds(), etc functions to display duration in different units:
moment.duration(1500).milliseconds(); // 500
moment.duration(1500).seconds(); // 1
Partial solution: https://jsfiddle.net/krlkv/xvstvfdb/3/
Just use mod:
day = day%24;
secs = secs%60;
and etc.
I have a function that will calculate time between two date / time but I am having a small issue with the return.
Here is the way I collect the information.
Start Date
Start Time
Ending Date
Ending Time
Hours
And here is the function that calculates the dates and times:
function calculate (form) {
var d1 = document.getElementById("date1").value;
var d2 = document.getElementById("date2").value;
var t1 = document.getElementById("time1").value;
var t2 = document.getElementById("time2").value;
var dd1 = d1 + " " + t1;
var dd2 = d2 + " " + t2;
var date1 = new Date(dd1);
var date2 = new Date(dd2);
var sec = date2.getTime() - date1.getTime();
if (isNaN(sec)) {
alert("Input data is incorrect!");
return;
}
if (sec < 0) {
alert("The second date ocurred earlier than the first one!");
return;
}
var second = 1000,
minute = 60 * second,
hour = 60 * minute,
day = 24 * hour;
var hours = Math.floor(sec / hour);
sec -= hours * hour;
var minutes = Math.floor(sec / minute);
sec -= minutes * minute;
var seconds = Math.floor(sec / second);
var min = Math.floor((minutes * 100) / 60);
document.getElementById("result").value = hours + '.' + min;
}
If I put in todays date for both date fields and then 14:30 in the first time field and 15:35 in the second time field the result is shown as 1.8 and it should be 1.08
I didn't write this function but I am wondering if someone could tell me how to make that change?
Thank you.
If I understand correctly, the only issue you are having is that the minutes are not padded by zeroes. If this is the case, you can pad the value of min with zeroes using this little trick:
("00" + min).slice(-2)
I can't see why 15:35 - 14:30 = 1.08 is useful?
Try this instead:
function timediff( date1, date2 ) {
//Get 1 day in milliseconds
var one_day=1000*60*60*24;
// Convert both dates to milliseconds
var date1_ms = date1.getTime();
var date2_ms = date2.getTime();
// Calculate the difference in milliseconds
var difference_ms = date2_ms - date1_ms;
//take out milliseconds
difference_ms = difference_ms/1000;
var seconds = Math.floor(difference_ms % 60);
difference_ms = difference_ms/60;
var minutes = Math.floor(difference_ms % 60);
difference_ms = difference_ms/60;
var hours = Math.floor(difference_ms % 24);
var days = Math.floor(difference_ms/24);
return [days,hours,minutes,seconds];
}
function calculate (form) {
var d1 = document.getElementById("date1").value;
var d2 = document.getElementById("date2").value;
var t1 = document.getElementById("time1").value;
var t2 = document.getElementById("time2").value;
var dd1 = d1 + " " + t1;
var dd2 = d2 + " " + t2;
var date1 = new Date(dd1);
var date2 = new Date(dd2);
var diff = timediff(date1, date2);
document.getElementById("result").value = diff[1] + ':' + diff[2];
}
Verify if number of minutes is less than 10 and if it is then append an additional zero in front. Follow similar approach for seconds.
This question already has answers here:
Check time difference in Javascript
(19 answers)
Closed 8 years ago.
I have tried to get the time difference between 2 different times and i am getting it correctly for hours and minutes. But if the second is greater than the first it will getting the problem. The time is displaying with negative data.
for example
Start time : 00:02:59
End time : 00:05:28
If i getting the difference between start and end time
00:05:28 - 00:02:59 = 00:3:-31
Which is not a correct value. I'm using the following script for getting this value.
var start_time = $("#startTime").val();
var end_time = $("#endTime").val();
var startHour = new Date("01/01/2007 " + start_time).getHours();
var endHour = new Date("01/01/2007 " + end_time).getHours();
var startMins = new Date("01/01/2007 " + start_time).getMinutes();
var endMins = new Date("01/01/2007 " + end_time).getMinutes();
var startSecs = new Date("01/01/2007 " + start_time).getSeconds();
var endSecs = new Date("01/01/2007 " + end_time).getSeconds();
var secDiff = endSecs - startSecs;
var minDiff = endMins - startMins;
var hrDiff = endHour - startHour;
alert(hrDiff+":"+minDiff+":"+secDiff);
anyone please tell me how to get the time difference between two times correctly even considering with seconds
Try doing this
var date1 = new Date(2000, 0, 1, 9, 0); // 9:00 AM
var date2 = new Date(2000, 0, 1, 17, 0); // 5:00 PM
if (date2 < date1) {
date2.setDate(date2.getDate() + 1);
}
var diff = date2 - date1;
// 28800000 milliseconds (8 hours)
You can then convert milliseconds to hour, minute and seconds like this:
var msec = diff;
var hh = Math.floor(msec / 1000 / 60 / 60);
msec -= hh * 1000 * 60 * 60;
var mm = Math.floor(msec / 1000 / 60);
msec -= mm * 1000 * 60;
var ss = Math.floor(msec / 1000);
msec -= ss * 1000;
// diff = 28800000 => hh = 8, mm = 0, ss = 0, msec = 0
this code should give 0:40 minutes, however in one way gives me 0:20 minutes, and in the other way gives me 1:40 minutes.
var t1 = '12:05'.split(':'),
t2 = '12:45'.split(':');
var d1 = new Date(0,0,0,t1[0],t1[1]),
d2 = new Date(0,0,0,t2[0],t2[1]);
document.write(d1+'<BR>');
document.write(d2+'<BR>');
var d = new Date(d1-d2);
// should give 0:40 minutes
document.write(d.getHours() + ":" + d.getMinutes() + '<BR>');
// 0:20 minutes
var d = new Date(d2-d1);
document.write(d.getHours() + ":" + d.getMinutes() + '<BR>');
// 1:40 minutes
Any ideas?
You can write your own function to calculate two dates difference and show the result in HH:MM format.
Because Javascript returns two date difference in timestamp & if we use that timestamp to get JS date object it creates totally new date.
Here is the sample dateDiff function :
function dateDiff(){
var start = '12:05'.split(':'), // hardcoded value for sample
end = '12:45'.split(':'); // you can pass start and end value as parameter to dateDiff function.
var startDate = new Date(0, 0, 0, start[0], start[1], 0);
var endDate = new Date(0, 0, 0, end[0], end[1], 0);
var diff = endDate.getTime() - startDate.getTime();
var hours = Math.floor(diff / 1000 / 60 / 60);
diff -= hours * 1000 * 60 * 60;
var minutes = Math.floor(diff / 1000 / 60);
return ( (hours < 9 ? "0" : "") + hours + ":" + (minutes < 9 ? "0" : "") + minutes);
}
Subtracting two dates gives you the number of milliseconds between them. Passing this to new Date gives you a new date object that many milliseconds from epoch.
If you want the number of minutes between two dates, just do this:
(d1-d2)/(60*1000);