I have an application that is using angular-ui-grid to render some form data. I have the form submission date coming from an api call, and wanted to know if there was anyway for me to compare that date to the current date in order to get days from submission within ui-grid. Or is this something that should be handled on the server side. The date coming from the API call is in the following format: YYYY-MM-DDTHH:MM:SS
Asked
Active
Viewed 257 times
1 Answers
0
If you want to compute the number of days between two dates (the difference):
- See: How do I get the number of days between two dates in JavaScript?
- Personally, I'd use a library like moment.js (or date.js)
For example:
var moment = require('moment');
var start = moment("2017-01-15");
var end = moment("2017-01-18");
console.log(start.diff(end, "days")); // 3
The date subtraction can be done either client-side or server-side, it's a technical design decision for you (and your team) to make. The trade-off:
- If you do this server-side, it will simplify your client-side code.
- However, if you do this date subtraction on the server-side, unless you do something clever, it's going to use the server's time zone and it will ignore the location of the user. So no matter where your users are in the world, they are all going to have the same value for the "current time", and therefore the same value for the date subtraction. It does not take into account time zones -- why might be bad, it depends if you care about the date difference value being 1 day ahead/behind in your application.
In case you didn't know: Date
takes into consideration the environment's system time settings. On the server-side, this is the server's time zone settings. Doing this date subtraction client-side will use the user's (browser's) system time zone settings.

Community
- 1
- 1

James Lawson
- 8,150
- 48
- 47