It's a little complicated to calculate delta time in js.
this is the pseudo-code,
var atime = "2010-12-05T08:03:22Z";
var btime = "2010-01-11T08:01:57Z"
var delta_time = btime - atime;
delta_time ?
I want to know exact date time between two time inputs.
is there any easy way to find out delta time?
var atime = new Date("2010-12-05T08:03:22Z");
var btime = new Date("2010-01-11T08:01:57Z");
var delta_time = btime - atime;
The value of delta_time will be the difference between the two dates in milliseconds.
If you're only interested in the difference, and don't care to differentiate between which is the later date, you might want to do
var delta_time = Math.abs(btime - atime);
A Date / Time object displays a time in a current situation (e.g. now() ). Displaying a difference of time is not part of a Date or Time object because the difference between e.g. May 1 and May 3 would result in, maybe, January 3, 1970, or maybe May 2, depends on how you start counting your delta on.
I would suggest putting your times into a timestamp which is a simple int in seconds. Do some substraction and voilá, there's your delta seconds. This delta can be used to apply to any other Object.
Related
I am trying to find time differences. Difference is NaN. What should I do?
currentTime.format() = 2016-12-07T11:43:19+03:00
pws.lastDataTime = 2016-12-07T08:35:14.4126931+00:00
var difference= currentTime.format() - pws.lastDataTime;
currentTime.format() = 2016-12-07T11:43:19+03:00
currentTime.format is a function. You can't assign it's return value to something.
currentTime.format() - pws.lastDataTime
I don't think the format function returns a number, but instead a string or an object. If you subtract anything from them, they return NaN (not a number). You need to either convert both to milliseconds and subtract one from the other, or calculate the year, month, day, hour, second and millisecond separately.
I don't know what denomination you want, so I'll just show you how to find it in milliseconds.
If already you have a date or two, you can use date.getTime().
var stackOverflowLaunchDate = new Date(2008, 8, 15);
var today = new Date();
var diff = today.getTime() - stackOverflowLaunchDate.getTime(); // milliseconds since Stack Overflow was launched
If you don't have (and don't need) a date object, use Date.now() to get millisecondds since epoch
var start = Date.now();
// ... Some time later
var diff = Date.now() - start; // milliseconds since start
I've a problem:
I have a website which should display a number in a specific div.
Now I want the number to increase by a specific amount every day, I am only allowed to use JavaScript and it should return the final number.
For example:
Today the number (start) is 100. Tomorrow it should be 110, the next day 120,... so the function should add 10 to the number each day.
I thought of a kind of loop but this would only work when I reload the site and wait a day :P Is there another option?
Thanks!
You may need to calibrate this a bit, but this should get you going
var startDate = new Date('2-10-2016');
var today = new Date();
var diff = Math.floor((today - startDate)/(1000*60*60*24))
var answer = 100 + diff * 10
If you know the number N that should be added each day, you can actually implement do, in pseudocode:
numberOfPassedDays = Today - initDay
resultToShow = N * numberOfPassedDays + numberAtInit
and calculate it every refresh of the page (in your example N=10 )
How do I calculate the difference in minutes given two strings. For example say I have
11:00
11:30
But of course the second string could be 12:11 so I can't subtract just the minutes.
first use javascript to convert the strings to time, then subtract, then convert back to strings
like this:
x = new Date("1/1/01 11:00")
y = new Date("1/1/01 11:30")
// now y-x has difference in milliseconds
// (y-x)/1000 is difference in seconds, etc
The data 1/1/01 is just being used as a dummy value, but the one thing you might have to worry about is are the times on different days, if so you will have to use 1/2/01 for the second time. Unless of course you always know the times are in the same day, but if they can cross "midnight" then you have to adjust for that.
You may want to use http://momentjs.com/ which will take care of the details for you.
When looking for getting metrics such as date , hour , minutes, seconds from the date difference, it's a lot easier to use basic notations as listed here
var x = new Date(new Date().getTime() + 11.5*60*60000); // adds 11 hours - 30 minutes
var y = new Date(new Date().getTime() + 11*60*60000); // adds 11 hours
alert(x.getMinutes() - y.getMinutes()); // gives the difference = 30
Here's an example : https://jsfiddle.net/DinoMyte/157knmgn/
In javascript application, I don't have Dates but only timings of a day in 24hrs format. How can I properly find the difference between them?
All my google search results are giving Date Time difference calculations only.
For example, If I want to find the difference between (4.15pm to 2.45 pm),
In my code I have,
var startTime = "14.45"; var endTime = "16.15";
Now var diffTime = endTime - startTime, which should give 1.30.
To get clarity on my question, you can refer my related question SO, to understand what I am trying to achieve.
Convert your string into the smallest unit required, which is minutes in your case. Do the arithmetic to get the result in minutes. Again do arithmetic to find hours and minutes from the result. You can also add logic to check if hours is zero and change to 24 in that case. But as comments point out, bare times cannot be compared if not of the same date.
function getMinutes(timeString){
var hours = parseInt(timeString.split('.')[0], 10);
hours = hours === 0? 24 : hours;
var minutes = parseInt(timeString.split('.')[1], 10);
var totalMinutes = (hours * 60) + minutes;
return totalMinutes;
}
var startTime = "23.45";
var endTime = "0.15";
var differenceInMinutes = getMinutes(endTime) - getMinutes(startTime);
var resultString = Math.floor(differenceInMinutes/60) + "." + (differenceInMinutes%60);
Another way can be by creating dummy dates using date constructor and keeping date part as some arbitrary date (same for end and start) and do common date comparison methods.
You seem to have 2 string variables in javascript. So talking about times and dates is really too early. They pretty much look like floating point numbers but if this was the case then the only arithmetic you could apply to floating point numbers is floating point arithmetic (+, -, *, /). So the first step for you would be to parse those string variables to the corresponding floating point numbers:
var startTime = '14.45';
var endTime = '16.15';
var startTimeFloat = parseFloat(startTime);
var endTimeFloat = parseFloat(endTime);
if (isNaN(startTimeFloat) || isNaN(endTimeFloat)) {
alert('Please input valid floating point numbers before being able to do any arithmetic on them');
} else {
var difference = endTimeFloat - startTimeFloat;
// Obviously here you should take into account the case where
// the start number is bigger than the end number in which case
// you would get a negative value for the difference. If you care only
// about the absolute value of the difference then you may use the
// Math.abs javascript method. Just like that:
// var difference = Math.abs(endTimeFloat - startTimeFloat);
alert(difference);
}
Here is my code.
var today = new Date();
var reqDate = new Date(today.getFullYear(),today.getMonth()-3, today.getDate());
var day = today-reqDate;
I want the 'day' should be something around 90; but it gives as some long integer.
The long integer is the number of milliseconds since midnight Jan 1, 1970. So in order to get the number of days you need to divide it. Code below:
var days = day/(1000*60*60*24);
You have got value in day variable as milliseconds, so divide it by 1000*60*60*24 to get day count.
Another thing, it will be a decimal value.
So you have to discard fraction value using floor function.
var days = Math.floor(day/(1000*60*60*24));