How to calculate the difference between both time and date in Javascript? - 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

Related

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')

Javascript: Calculating time duration on ajax response

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.

How to compare timespan in javascript after timezone conversion using moment js

I've a list of timespan(object list actually), like 2:00, 15:00, 18:00 etc, it is in utc.
Now i want to convert this time slot back to CST and then sort it, as i want my time sorted in cst.
For timezone conversion i needed temporary date. so i choose current utc date by
moment.utc(mytimespan). and performed the timezone conversion by .tz("CST").
So list is converted to 20:00,9:00, 12:00
Here please note that i got 20:00 in first place instead of last place in the list.
This is due to date part of moment which went in back date.
All here i want is my timespan in sorted form without any effect of date.
please me to find a way to do it without string conversion!
Thanks
Update
my currently working code using string conversion
TimeSpanDetails.sort(function compare(a, b) {
return moment(moment.utc(a.startTime).tz("CST").format("HH:mm"),"HH:mm").isAfter(moment(moment.utc(b.startTime).tz("CST").format("HH:mm"),"HH:mm")) ? 1 : -1;
});
Now i want to do it without string conversion using format
A few things:
A "time span" usually refers to a duration of time, not a time-of-day. These are two very different concepts that are sometimes confused. Consider:
A timespan of 99 hours is perfectly valid, but "99:00" is nonsensical as a time-of-day.
Due to daylight saving time and other time zone transitions, a timespan can't necessarily be thought of as "time since midnight" because midnight may or may not exist, or some other hour of the day may be absent or repeated.
Time spans can be negative in some programing languages, usually representing a period before a given point in time.
The tz function in Moment.js takes IANA time zone names. You should not use CT or CST, but rather America/Chicago, for example. However, time zones are completely unrelated to time spans, so you should not be applying them at all. You do not need moment-timezone.
Moment represents time spans in Duration objects. You can parse them from strings like so:
var d = moment.duration('99:00');
Duration objects convert numerically to milliseconds, so they are comparable like so:
var a = moment.duration('00:00');
var b = moment.duration('01:00');
var c = a < b; //=> true
Moment does not have a strongly typed object for a time-of-day, but you can use Moment in UTC mode so that it does not have DST transitions, and then just let it use the current day. HOWEVER:
This would assume that all time-of-day values you have should be evaluated on the same date.
This may or may not be the case.
Consider that if all you have is time-of-day and don't know what dates they're from, then the values ['23:00', '00:00'] may be sorted already and only one hour apart, or perhaps they're out of sequence and they are 23 hours apart.

function reminding me about a date

I have a function and it represent a date that is 2 weeks off from start date, counted by each passing Thursday, but excludes the Thursday of the week the date was made.
function GetThursdayIn2Weeks(date)
{
var day = date.getDay();
// Add 2 weeks.
var newDate = new Date(date.setTime(date.getTime() + (14 * 86400000)));
// Adjust for Thursday.
var adjust = 4 - day;
if (adjust <= 0) // Might need to be changed - See comments!
adjust +=7;
// Apply Thursday adjustment.
newDate = new Date(newDate.setTime(newDate.getTime() + (adjust * 86400000)));
return newDate;
}
How would I make this set off a different function every day that passed, starting a week after the beginning of the process, remind me about the due date coming up, but before the end of the date of the process?
You can use setTimeout() to execute a reminder after a set time. However, the problem is that your javascript environment will probably not keep running for such long times, be it node.js or your browser.
I would suggest those mechanisms :
store your target date in localstorage after calculating it with your given code
define a function that will use setTimeout() to define the next occurrence of the reminder for a given target date
when the page is loaded, use that function for each date stored in the localstorage
when a date is added to the localstorage, or a given target date reachs one of its reminders, call the function for this specific date
The mentioned function should set a timer for the first day that is at the same time greater than the current date, greater than the day 1 week before the target date, and lower than the target date.
Here is an 'hopefully) working JSFiddle.

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