Reputation: 4266
I have a form where the user has to input one begin date and one end date. After that, I have to compute the result of this date to have a status on the UI:
How can I compute that easely? I have date in the form with that format: 04/21/2015.
Do you recommand me to compare date by changing format (04/21/2015 => 20150421) and compare? Or use Date of JS? or anything else?
Thanks in advance
Upvotes: 0
Views: 151
Reputation: 646
function getStatusByDate(startDate, endDate) {
var currentTime = new Date().getTime();
var startTime = new Date(startDate).getTime();
var endTime = new Date(endDate).getTime();
console.log(startTime, endTime);
if (startTime < endTime && endTime < currentTime) {
return ('DONE');
} else if (startTime < endTime && endTime > currentTime && startTime < currentTime) {
return ('IN PROGRESS');
} else if (startTime < endTime && startTime > currentTime) {
return ('TO BE DONE');
} else {
return ('INVALID INPUT');
}
}
Upvotes: 0
Reputation: 46
have you tried http://momentjs.com/ . Real easy to use, conversion and formatting could save you time adding further JavaScript code however it is offset by adding the library.
Upvotes: 0
Reputation: 1365
Get two date objects like this:
// parse a date in mm/dd/yyyy format
function parseDate(input) {
var parts = input.split('/');
// new Date(year, month [, day [, hours[, minutes[, seconds[, ms]]]]])
return new Date(parts[2], parts[0]-1, parts[1]); // Note: months are 0-based
}
Then compare using usual operators < > ==
Upvotes: 1
Reputation: 11750
You can create a date object for the input date and another date object for the current date and then, simply compare them.
Check the dateObject in order to provide the correct date string format:
var inpDate = $('input').val();
var d = new Date(inpDate);
var today = new Date();
if (inpDate > today) {
return "TO BE DONE";
} else ....
Upvotes: 0