Currently, I am in the process of developing a function that compares the current date with an expiration date. The input, expireStamp, is represented as a timestamp measured in milliseconds.
compDate = function(expireStamp) {
// Convert the timestamp of expireStamp to a readable Date object
var expireDate = new Date(expireStamp);
var notifyDate = new Date().setDate(expireDate.getDate() - 30);
var today = new Date(); // Current date
console.log("Today: " + today);
console.log("Notify: " + new Date(notifyDate));
console.log("Expire: " + expireDate);
if(today.getTime() <= notifyDate) {
// The date is still valid
return "good";
} else {
// The date may be expired
if(today.getTime() > notifyDate && today.getTime() <= expireDate.getTime()) {
// The date is soon to expire
return "soon";
} else if(today.getTime() > expireDate.getTime()){
// The date has already expired
return "fail";
}
}
}
In this scenario, we need to compare today's date against two dates: the expiration date and the notification date. The notification date is set as 30 days before the expiration date. The issue I am facing revolves around the notification date calculation. If the expiration date is set too far into the future, the notification date behaves unexpectedly. Here are some test cases:
> var exp = new Date(1409362782000)
undefined
> exp
Fri Aug 29 2014 21:39:42 GMT-0400 (Eastern Daylight Time)
> var notify = new Date().setDate(exp.getDate() - 30);
undefined
> notify
1396183229815
> var test = new Date(notify);
undefined
> test
Sun Mar 30 2014 08:40:29 GMT-0400 (Eastern Daylight Time)
I have set the expiration date as August 29th (considering today is April 4, 2014) using a timestamp in milliseconds. As you can see, exp is displaying the correct expected date.
The issue arises when calculating the Notify date which should ideally be 30 days prior to exp. However, notify returns March 30th, which is significantly more than 30 days before August 29th. This problem occurs mainly with dates further in the future compared to the current date.
To resolve this problem, I aim to ensure that the notify date accurately reflects being 30 days before the expiration date.