How to calculate the time difference in javascript? - javascript

I have a time entry as follows:
FromTime:73400secs in Time format: 20:23
ToTime:73624secs in Time format: 20:27
When taking difference of the secs it is : 224secs in Time Format : 00:03:44
but when taking difference of the Time Format it is: 00:04
Why is this difference happening?
Which is the correct way to show the difference in the fromTime & toTime?

Related

Simplest way to find out how many milliseconds until a specific time in a specific timezone (taking DST into account)?

I have a piece of code which finds the difference between two dates(in the format of yyyy-MM-dd hh:mm:ss) . This code is run in multiple servers across the globe. One of the two dates is the current time in that particular timezone where the code is being run(server time) and another is the time obtained from a database. if the difference between these two times is greater than 86400 seconds(1day), then it should print "invalid" else, it should print "valid".
Problem im facing with the code is when I run it on my local, its working fine, but when i deploy it onto a server in US, its taking GMT time into consideration and not local time.
Wherever the code is run, I want the difference between current time and time fetched from the database, and if its greater than 86400 seconds, i want to print invalid. How to achieve this in java?
PS: I tried with Date object, but its considering GMT only everywhere.
I would use GMT everywhere and only convert to the local times for display purposes.
To find the difference, convert both times to the same timezone (say GMT) and take the difference.
You can do it by the below example code.
Date date = new Date();
DateFormat formatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z");
formatter.setTimeZone(TimeZone.getTimeZone("CET"));
Date date1 = dateformat.parse(formatter.format(date));
// Set the formatter to use a different timezone
formatter.setTimeZone(TimeZone.getTimeZone("IST"));
Date date2 = dateformat.parse(formatter.format(date));
// Prints the date in the IST timezone
// System.out.println(formatter.format(date));
Now compare date1 with date2
First, I concur with Peter Lawrey's answer up there. It is usually good practice to store all time in the database for a single zone, and render it with offset for the user based upon the user's locale.
To find the difference, use the method getTime() to get the time in milliseconds from the epoch for each date. The calculation for the difference of 1 day is then 86400 * 1000 milliseconds. Or, perhaps, store the time in milliseconds from epoch in the database, and use a DB procedure/function at the time of retrieval.
Hope this helps.

How to time duration calculate in react js

I want to calculate the time duration with the current time and another time. In my problem the current time and all ready time in data base time format are different, this format is given format: 2020-11-07 , 22:52
but now time format is 30/10/2020 , 20:50:34 . So I have a problem with this diffrent format.
You can convert both values to timestamps (in milliseconds) from their own formats and then calculate the difference in milliseconds with simple math.
If you are having trouble parsing the values, you can check Date documentation. An example can be:
let birthday = new Date('1995-12-17T03:24:00')

momentjs time comparison with timezone

I am trying to compare two times:
Time A : A datetime which I know is in 'America/Chicago' timezone
Time B : A datetime for current time which I want to convert to 'America/Chicago' and compare to Time A.
I am doing this simple test and unable to understand why all the below give the same result:
moment('05/31/2018 03:25:00').tz('America/Chicago').diff(moment().tz('America/Phoenix'), 'minutes')
Result: 111
moment('05/31/2018 03:25:00').tz('America/Chicago').diff(moment().tz('America/Los_Angeles'), 'minutes')
Result: 110
moment('05/31/2018 03:25:00').tz('America/Chicago').diff(moment().tz('America/Chicago'), 'minutes')
Result: 110
moment('05/31/2018 03:25:00').diff(moment().tz('America/Chicago'), 'minutes')
Result: 110
Note: my system current time is 01:25 PST. So, I believe moment().tz('America/Los_Angeles') should mean 01:25 PST.
Please correct where I am going wrong. I am basically trying to compare a time in CST with current time in PST and find out what's the difference in minutes in real time.

How to calculate the difference between both time and date in Javascript?

Start time = 03/30/2017 15:55
End time = 03/31/2017 20:55
What I want is Elapsed time should be the difference between both start time and end time and it should display both the date and time.
date.parse gives you the miliseconds (since 1970).
Just compare those numbers then. If its higher its a later date.
For more questions visit : https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Date/parse

Adding hours and minutes to a date with JavaScript

I am building a meeting calendar based on time zones around the world. My problem is how to add or subtract the time zone from the user selected date in JavaScript.
For example, on the select form, the user will select the date from a form: I would then get the results and convert to a date as below...
var ldSelectDate = new Date(document.form1.year.value,
document.form1.month.value,
document.form1.day.value);
I would like to add 12 midnight to this object.
I then read in an XML that gets the time zone difference in a string between two cities: such as "+0430" for Kabul or "-0400" for New York (on daylight savings time). This is based on GMT,.
I then calculate the time zone difference between the two cities: which will return the string of "830". (I assume I have to return this as a date object?). I got this part done returning a string. I'm working with strings.
I then want to loop through the 24 hours of the day, set Kabul at 12 midnight and then loop through. I can most likely figure this out - that is, set the date with the whole hours as I loop.
My problem is painlessly subtract the "830" from Kabul to see what the meeting time will be in New York.
It will be ideal if I can just subtract the hours and the minutes from the Kabul time. I noticed on here someone subtracting the hours in JavaScript, but not the minutes. BTW, that post didn't work for me.
I did this with strings without the minutes, but I mess up with the minutes. There has to be an easier way.
I would take a solution in either native JavaScript or jQuery.
Again, I need to subtract/add the time zone difference in hours and minutes to a certain date.
date.setMinutes(date.getMinutes()+minutesToAdd);
The above code will set your date object ahead by the amount of minutes in the minutesToAdd variable
Easiest would be to calculate the minutes for that time delta then do a minutes delta.
date.setMinutes(date.getMinutes() - (hours*60+minutes))

Categories