Difference between two time object - javascript

Am trying to get number of hours between two time objects. Am using 24h time format. If the difference in hours is higler than 24 hours, I want to increase price for some cars.
User can select start time and end time from input typte=time: 10:25 / 23:45
Which is the best approach to do this. I google but that examples i dont understand clearly...

Just subtract them if they are date objects, if they are not make them.

You can get the amount of hours between two dates with:
var date1 = new Date('2016,03,10');
var date2 = new Date();
var hoursBetween = ((date1-date2)/1000)/3600;

Related

Finding number of days between two dates in javascript/redux

let oneDay = 24*60*60*1000;
let firstDate = new Date();
let secondDate = this.props.eventData.date
let finalSecDate = new Date(secondDate)
var timeDiff = Math.abs(firstDate.getTime() - finalSecDate.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
I am trying to calculate the number of days between two dates using javascript in a redux project. (My second date variable above is based on the date that a user enters and then I am changing it into a new Date format.) The above code works but when the event has passed the the number of days until the event is still coming up as positive number of days. Can someone please help me distinguish whether or not the date has passed so I can get the negative number of days.
I appreciate any help you can give, thank you !
I would recommend using Moment.js, since they have a number of date functions that will become useful as you play around with dates more.
Here's what it would look like if you wanted to find the difference between two dates:
var present = moment();
var end = this.props.eventData.date;
present.diff(end, 'days') // 5
The diff function finds the difference between the two dates. It also solves your problem of returning a negative value if the date already passed.
var present = moment();
var past = moment('2014-02-03 12:53:12');
past.diff(present, 'days') // -1379

How to create a Date object from year, month and date strings?

I'm currently just practising JavaScript and I'm trying to create a programme that calculates how many days there are until your next birthday.
I have seen on this site that there is a daysBetween function that I can use to tell the time difference between two dates (I just need to turn these dates into the millisecond value since the 1st Jan 1970).
The problem is that, although one of those dates is the current date (which is easy to convert into its millisecond value), the second is derived from answers the user inputs as a string into a prompt command (there are three different boxes that ask for the year, month and date of their birth). Is there a way I can convert these input strings into a date format that I can then use to find the days between today's date and their next birthday?
Thanks and sorry if this is a stupid question!
Remember in JS that months are indexed from 0, so January === 0.
var date = new Date('2017','5','25'); would be today, assume you have something like:
var userDd = '25';
var userMm = '6'; // User doesn't know the months are indexed at 0.
var userYy = '2017';
var date = new Date(userYy, userMm - 1, userYy);
date.getTime(); // returns the milliseconds value.

JavaScript for date comparison

I want to write a JavaScript to compare today's date with one field in a MsSql database assuming that the field name is "document_issuedate" and put this condition.
if today's date - document_issuedate > 30 then alert
How to calculate date difference in javascript
This question is similar, and appeared after a quick search. Basically, as long as you make the dates into a date object you can simply just subtract them like you've shown
Edit
Here's a better link then with more explanations and the appropriate snippet from that page.
The oneDay variable is just there for convenience.
The date objects are in Y,m,d format.
Using getTime() on the date objects makes it so the difference gives a numeric value (in milliseconds) instead of a date object. Then dividing it by oneDay converts the milliseconds into days which is what you want. Math.abs makes sure you always have a positive number difference and Math.round makes it a whole number instead of a decimal
How to calculate the number of days between two dates using JavaScript?
var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
var firstDate = new Date(2008,01,12);
var secondDate = new Date(2008,01,22);
var diffDays = Math.round(Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay)));

Save time in mongodb

I have tried to save a specific time into my mongodb database
with the javascript date object like:
var currenttime = new Date();
currenttime.setHours(14);
currenttime.setMinutes(37);
db.test.insert({time: currenttime});
however I have noticed that not only are the hours and minutes saved,
but also the date. I am searching for a way to only save the hours and
minutes, but in a way that I can still do less than greater than operations
on it. Is there a way to do this?
MongoDb Date is only 64bits, on the other hand if you will store your time as just 2 32 bit integers (hours and minutes) you will be already using these 64 bits. If you will save them as a 4 letters string, it will be even more.
So you can not gain space advantage. But you will lose advantage of querying your data. It will be harder to find all elements that are bigger than particular time with 2 numbers format and even harder with strings.
So I would save them as dates. If you really need only time - and need to query by this time, you can do the following trick: make all dates the same. For example:
var a = new Date(); // gives current date and time (note it is UTC datetime)
a.setYear(2000);
a.setMonth(0);
a.setDate(1);
db.test.insert({time: currenttime});
This way all the elements will have the same date and different time. In such a way you sort them properly. Also if you need to find all the elements where time is smaller than a particular time, you can quickly create a date object with year/month/day (2000/0/1) and query your data properly.
You can consider to save number of minutes as an integer field.
For example, 8:30 will be converted as 8 hour * 60 minutes + 30 minutes = 510 minutes. Save 510 as an number in MongoDB.
Basically you can try these two options to save time:
Save time as type: String
const userInput = '05:20';
const hours = userInput.slice(0, 2);
const minutes = userInput.slice(3);
Save date time in type: Date
But in second option you have to create Date object and set hours and minutes:
const date = new Date(dateString);
date.setHours(hours, minutes);

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