starttime=(new Date()).getTime();
endtime=(new Date()).getTime();
(endtime-starttime )/1000
will give a value.What is this value and why is it divided by 1000
Well, in this particular case the value will be 0.
you need to divide it by 1000 because time is represented in miliseconds, so to get the seconds you need to perform the transformation 1s = 1000ms
That code is calculating the number of seconds that have elapsed between two dates. The division by 1000 is there because the getTime() method returns a value measured in millseconds.
The code is actually needlessly long-winded. To get the milliseconds that have elapsed between two Date objects, you can just use the - operator on the Dates themselves:
var start = new Date();
// Some code that takes some time
var end = new Date();
var secondsElapsed = (end - start) / 1000;
value=millisecond delta, it is divided to turn the delta into seconds
Date getTime() gives the number of milliseconds since 1970 (Epoch)
Divide the difference by 1000 and you get seconds
Related
I have future date and now date. Both of this dates are always in same day but with just different hours. My goal is to get the difference of seconds between the future date and now date as my countdown value for a timer. The problem is when I calculate I'm getting inaccurate results.
In my research formula of converting milliseconds to seconds is millis / 1000.0 but non of this returns accurate countdown result;
My code
let now = (new Date().getTime() / 1000.0);
let futureDate = (new Date('2022-04-01T17:41:47.000Z').getTime() / 1000.0);
let difference;
difference = (futureDate - now); // not accurate
difference = parseInt(difference, 10); // not accurate
I would like the solution to work normal on all timezones and to inherit local system timezone instead of future date timezone.
Any help will be appreciated so much.
You should add the system timezone, like this:
let date = new Date('2022-04-01T17:41:47.000Z');
let now = new Date().getTime() / 1000.0;
let futureDate = (date.getTime() + date.getTimezoneOffset() * 60000) / 1000.0;
let difference;
difference = (futureDate - now); // not accurate
difference = parseInt(difference, 10); // not accurate
console.log(difference);
I want to check if you know that date formats like "0000-00-00T00:00:00.000Z" are always recognized as universal time (UK time).
Try using +HH:MM instead of the last Z character.
For example, if you are in the US East, it would be "2022-04-01T17:41:47.000-05:00".
The timestamp '2022-04-01T17:41:47.000Z' will be parsed as offset +0 (aka UTC or GMT) due to the trailing "Z", denoting a +0 offset. The difference between now and then in milliseconds is:
let diff = new Date('2022-04-01T17:41:47.000Z') - Date.now();
where a negative value for diff means the timestamp is in the past. To convert the difference to seconds, divide the result by 1,000.
If run at exactly the same time, the value for diff will be the same regardless of system settings for local offset (allowing for clock accuracy of course).
For the timestamp to be parsed as local, remove the Z:
let diff = new Date('2022-04-01T17:41:47.000') - Date.now();
However, that shifts the instant in time represented by the timestamp by the local offset, so will return a different value for diff for each system with a different offset. That doesn't seem like a sensible thing to do given the timestamp has an offset and so is intended to represent a single instant in time, not one of many different instants (as many as there are different offsets, perhaps hundreds if historical offsets are included) depending on the host offset.
I am trying to calculate the time duration of a tasks, that I get from an ajax response.
Following are my table values:
Jobid techid, techtype, notes, starttime, stoptime
1 1 Brakes Break disc needed to be changed 2020-07-16 13:00:00 2020-07-16 13:40:00
1 2 Oil Change Replaced oil 2020-07-17 08:00:00 2020-07-17 09:00:00
1 3 Cleaning Cleaned the vehicle 2020-07-17 10:00:00 2020-07-17 10:30:00
On my ajax response, in the above case, I am getting 3 objects each having the start time, and stop time. I want to calculate total time spent in hours and minutes.
Is there an easy way to calculate the total duration?
With a string like 2020-07-16 13:00:00 you can construct a JS Date and get the milliseconds since the UNIX epoch with getTime() like so
new Date('2020-07-16 13:00:00').getTime()
Or, if you prefer, as pointed out by #Yousaf in the comments you can actually just use the - operator with Dates directly and get the millisecond difference
// resolves to 3600000, or 1 hour in milliseconds
new Date('2020-07-16 13:00:00') - new Date('2020-07-16 12:00:00')
Using that, you can get the difference in milliseconds between any two dates, and convert that to hours / minutes / whatever with straightforward arithmetic.
You can simply use Date to construct a date and then minus the start time from the end time.
Here I use getTime to get the millisecond difference, divide by 1000 to get seconds and divide by 60 to get minutes.
You could also use getMonth and such if you have bigger differences.
const starttime = '2020-07-16 13:00:00'
const stoptime = '2020-07-16 13:40:00'
const duration = new Date(stoptime) - new Date(starttime)
console.log(duration / 1000 / 60)
[UPDATE]
I think you can check this answer, but basically you should convert each date to js Date, get the milliseconds and just calculate endtime - startime.
const timelapse = new Date(endtime).getTime() - new Date(startime).getTime();
From there, you transform that in the unit you need (e.g: seconds = milliseconds/1000);
Sorry, my bad for writing fast.
I am measuring quite precise time and have to convert miliseconds into minutes, seconds and hundreths of a second. Like this: MM:SS.hh
Any help is appreciated!
Here's one approach.
Let's say the number of milliseconds (ms) you need to convert is 123,215.
Let's start with the number of minutes MM.
Number of milliseconds in a minute = 1 minute * 60 seconds * 1000 milliseconds
= 60,000
123,215 / 60,000 = 2 (truncate after dividing)
Hence, there are two full minutes within the original number of milliseconds.
MM = 2.
Next, remove a number of milliseconds equivalent to MM from the original number of milliseconds.
123,215 - (2 * 60,000) = 3,215
Use 3,215 to calculate the number of seconds SS.
Repeat a similar process here.
Number of milliseconds in a second = 1 second * 1000 milliseconds
= 1,000
3,215 / 1,000 = 3 (truncate after dividing)
SS = 3.
Remove a number of milliseconds equivalent to SS from the original number of milliseconds.
3,215 - (3 * 1000) = 215
What you're left with now are what you describe as your hundredths.
To be more accurate, these are the thousandths that didn't fit into whole seconds.
So the result of your conversion is :
02:03:215
Hi I'm passing a unixtimestamp to a javascript IF statement, can anyone tell me how to generate a unixtimestamp one minute in the future with javascript.
Anyhelp would be helpful.
Thanks
The JavaScript Date object has a getTime() method that returns milliseconds since 1970. To make this look like a UNIX timestamp, you need to divide by 1000 and round (with Math.floor()). Adding 60 get's your one minute ahead.
var d = new Date();
var unixtimeAdd60 = Math.floor(d.getTime()/1000)+60;
UNIX time is just the number of seconds since 1970-01-01Z. So just add 60 you'll get a timestamp one minute later.
JavaScript Date object's getTime returns the number of milliseconds since midnight Jan 1, 1970.
Try this.
var oneMinLater = new Date().getTime() + 60 * 1000;
var d = new Date();
d.setTime(oneMinLater);
Another way to get the unix timestamp (this is time in seconds from 1/1/1970) in a simple way its:
var myDate = new Date();
console.log(+myDate + 60); // you just sum the seconds that you want
// +myDateObject give you the unix from that date
Hii,
I have an input time in millisecounds. I want to include a digital stop watch in my application.i.e The time will dynamically change like a digital clock in every seconds.
You can divide the milliseconds by 1000 to get the seconds, then by 60 to get the minutes and by 60 again to get the hours. Or, even better, use modulo:
hours = parseInt(milliseconds / 3600) % 24;
minutes = parseInt(milliseconds / 60) % 60;
seconds = (milliseconds / 1000) % 60;
However if you want a time like 02:00 PM you must know from what time you started to count the milliseconds (ie, what time is it when the milliseconds are "0").
I'm not sure exactly what your question is, as in what specific parts you need help with. Even if you're not that familiar with Javascript, this is fairly simple to do "manually" by just dividing by each increasing factor and taking the remainder, e.g.:
var input = ...; // your input time
var millis = input % 1000;
input /= 1000;
var seconds = input % 60;
input /= 60;
var minutes = input % 60;
input /= 60;
var hours = input % 24; // I presume this will be less than 24 anyway)
var entireTime = hours + ':' + minutes + ':' + seconds;
An alternate way to do this would be to create a Date object passing the input time into the constructor; this would then represent that number of milliseconds past the epoch and so printing out its value would include the given time. Depending on what date formatting frameworks you have available this might be a more straightforward method - and it would certainly allow more flexibility in terms of manipulating the value.
Just a thought - make sure that you fully understand what the input actually is. It's relatively unusual to give an input time in milliseconds; I'd expect that such an input would actually be a duration. This admittedly could be the number of milliseconds past midnight, but do be sure that it's not the number of milliseconds past some other arbitrary starting point.
The prefix milli is 10^-3. Thus one millisecond is one thousandth of a second, 60 thousandth of a minute and 3600 thousandth of an hour. So 1,000 milliseconds is one second, 60,000 milliseconds is one minute and 3,600,000 milliseconds is one hour.
This means, devide the number of milliseconds by 1000 and you get the number of seconds, by 60,000 and you get the number of minutes, and by 3,600,000 and you get the number of hours.