If you are looking to calculate the number of days between two dates (the difference):
- Check out this helpful resource: How do I get the number of days between two dates in JavaScript?
- Personally, I recommend using a library like moment.js (or date.js)
Here is an example for your reference:
var moment = require('moment');
var start = moment("2017-01-15");
var end = moment("2017-01-18");
console.log(start.diff(end, "days")); // 3
The process of subtracting dates can be done on either the client-side or server-side, depending on your technical requirements and preferences. Here are some considerations to keep in mind:
- If you choose to perform the calculation server-side, it will simplify your client-side coding.
- However, when conducting date subtraction on the server-side, it will default to using the server's time zone setting, disregarding the user's location. This means that users worldwide will see the same result for the date difference. While this may pose challenges related to time zones, the impact on your application depends on whether this discrepancy matters to you.
It's important to note that the Date
function considers the system time settings environment. On the server-side, these settings reflect the server's time zone, while performing date subtraction client-side takes into account the user's browser-based time zone settings.