Copy link to clipboard
Copied
I have two text fields, one where the user will enter the date of manufacture (dd-mmmm-yyyy) whilst the other one is for Expiry date (mmmm-yyyy), which has to be calculated manually.
The expiry date is by default 2 years. If date of manufacture is between 1 - 14 day of the month, expiry has to be the previous month (E.g, Date of manufacture: 05 July 2016; Exp: June 2018 || Date of manufacture: 25 July 2016; Exp: July 2018).
I have found a code to add a number of days however I have an issue with the leap year. Can someone provide me with a code? I am new to JavaScript and have no experience.
Script is as follows:
var strStart = this.getField("DateStart").value;
if(strStart.length)
{
var dateStart = util.scand("dd mmmm yyyy",strStart);
var oneDay = 24 * 60 * 60 * 1000;
var dueMillis = dateStart.getTime() + 716 * oneDay;
var dueDate = new Date(dueMillis);
event.value = util.printd("mmmm yyyy",dueDate);
}
else
event.value = "N/A";
Copy link to clipboard
Copied
What exactly is the issue with the leap years?
Copy link to clipboard
Copied
The problem is for 15 February of every leap year (e.g. 2016). The expiry date is showing January 2018 instead of February 2018.
For the rest of the months and those years which are not leap years, it is working perfectly.
Apologies for not specifying this in my original post.
Thanks,
Andrew
Copy link to clipboard
Copied
OK, but your code doesn't really do what you described, though. It just adds 716 days to the original date. I don't see the logic you mentioned about changing the expiry date to the previous month if it falls on the 1st to the 14th. Is this your full code?
Copy link to clipboard
Copied
Yes it works fine and that is the code. I am not sure if I can attach the file to show you.
Copy link to clipboard
Copied
The problem here isn't really the JavaScript. It just does what it's told - adds 716 days to the current day, and uses that as an expiry date. As you've found, this isn't correct. Nor is it a question of just adding a different number. There isn't a fixed number because years are different lengths (365 or 366 days).
So, before you can write a line of code you have to know what you will actually do instead. If you aren't mathematically minded, consider what you would do if given a date, to get the expiry date. What exactly would you do?
Copy link to clipboard
Copied
As TSN suggested, you need to rethink your approach. At the end, you will not have to do any complex date calculations - or take leap years into account. Take a look at the "Date" object in JavaScript: Date - JavaScript | MDN
What you need are the methods getDate(), getMonth() and getFullYear() to get the day, the month and the year. There are complementary methods to set these properties: setDate() (which you do not need because you are only interested in the month and the year), setMonth() and setFullYear(). If you check the documentation for setFullYear(), you will find that you can also pass in a month, so you only need to use setFullYear().
Copy link to clipboard
Copied
Thanks to both TSN and Karl for your help...
However, I do need the date as the expiry date is built on it. If a product is manufactured before the 14th date of the month (E,g, 10 May 2016), the expiry date has to be the previous month (i.e. April 2018) while if the product is manufactured after that date the expire is May 2016.
Would I need to use an if statement for this to work properly?
Copy link to clipboard
Copied
What I mean is that you need to describe the exact steps you'd follow to do this task by hand. only when you have the exact steps can you write actual code that works. Some tasks are obvious to humans from the description but that doesn't help The computer.
Copy link to clipboard
Copied
Hi,
I changed the code after your suggestions however it is still not functioning. The code is as follows:
var d = this.getField("DateStart").value;
var djs = util.scand("dd mmmm yyyy", d);
if (d != "" && djs != null) {
var day = djs.getDate();
var month = djs.getMonth();
var year = djs.getFullYear();
if (day <=14) {
var de = month + "/" + year;
var dejs = util.scand("mmmm/yyyy", de);
var month = djs.getMonth() -1;
var year = djs.getFullYear() + 2;
event.value = util.printd("mmmm-yyyy", dejs);
}
else {
if (day >=15) {
var de = month + "/" + year;
var dejs = util.scand("mmmm/yyyy", de);
var month = djs.getMonth();
var year = djs.getFullYear() + 2;
event.value = util.printd("mmmm/yyyy", dejs);
}
}
}
The problem with the code is that "var month = djs.getMonth() -1;" and "var year = djs.getFullYear() + 2;" are not working. The date display is working fine however there is no addition nor subtraction of the year/month. Can someone tell me what is wrong with the code?
Thanks a lot for your help.
Copy link to clipboard
Copied
Hi all,
After posting the above, I did some further modifications to the code and it is working fine. Thanks all for your help.
Copy link to clipboard
Copied
Once you compute the values for the revised year and month, you need to adjust or set the year and month for the date object. You can do this with the "setFullYear" method which can set the full year, month, and date.
Since the OP found a solution, this is for anyone that comes upon this post so they can see the solution.
var d = this.getField("DateStart").value;
var djs = util.scand("dd mmmm yyyy", d);
if (d != "" && djs != null) {
var day = djs.getDate();
var month = djs.getMonth();
var year = djs.getFullYear();
if (day <=14) {
var de = month + "/" + year;
var dejs = util.scand("mmmm/yyyy", de);
var month = djs.getMonth() -1;
var year = djs.getFullYear() + 2;;
// set date object to new year and month
dejs.setFullYear(year, month);
event.value = util.printd("mmmm-yyyy", dejs);
}
else {
if (day >=15) {
var de = month + "/" + year;
var dejs = util.scand("mmmm/yyyy", de);
var month = djs.getMonth();
var year = djs.getFullYear() + 2;
// set date object to new year and month
dejs.setFullYear(year, month)
event.value = util.printd("mmmm/yyyy", dejs);
}
}
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now