• 0

[HELP JAVASCRIPT] Need help modifying a JavaScript file that inserts a date based on another field


Question

We have the following JavaScript for a Caspio database that inserts a date of a year ahead into a field before submission based on the previous field.

 

function setDate()
{
var stringDate = document.getElementById("InsertRecordDATE_SUBMIT").value;
var myDate = new Date(Date.parse(stringDate));
myDate.setFullYear(myDate.getFullYear()+1);
var myMonth = myDate.getMonth() + 1;
if (myMonth<10) {myMonth = "0" + myMonth;}
var myDay = myDate.getDate();
if (myDay<10) {myDay = "0" + myDay;}
var myYear = myDate.getFullYear();
var result = myMonth + "/" + myDay + "/" + myYear;
document.getElementById("InsertRecordEXPIRATION_DATE").value=result;
} 

document.getElementById("caspioform").onsubmit=setDate;

  

Basically we have have a field where users enter a date, and the JavaScript above inserts a date of a year ahead on a second field before form submission.

 

Can someone modify it so that the date inserted is six months instead of one year.

 

My JavaScript knowledge is null so TIA   

Link to comment
Share on other sites

7 answers to this question

Recommended Posts

  • 0

Also i was given this example elsewhere

 

var sixMonths = 1000 * 60 * 60 * 24 * 180,
    newDate = new Date(myDate.valueOf() + sixMonths);

 

But again, I don't know how to incorporate that into what we have already. 

Link to comment
Share on other sites

  • 0

@Lare2, the big problem with this is how to calculate 6 months from the date.  If someone enters March 31 as an example, what is 6 months from that, since there is no September 31st.

 

If you don't care about the logistics of things, you can do something like this.

 

function setDate()
{
var stringDate = document.getElementById("InsertRecordDATE_SUBMIT").value;
var myDate = new Date(Date.parse(stringDate));
var sixMonths = 1000 * 60 * 60 * 24 * 180,
myDate = new Date(myDate.valueOf() + sixMonths);
var myMonth = myDate.getMonth() + 1;
if (myMonth<10) {myMonth = "0" + myMonth;}
var myDay = myDate.getDate();
if (myDay<10) {myDay = "0" + myDay;}
var myYear = myDate.getFullYear();
var result = myMonth + "/" + myDay + "/" + myYear;
document.getElementById("InsertRecordEXPIRATION_DATE").value=result;
} 

document.getElementById("caspioform").onsubmit=setDate;
Link to comment
Share on other sites

  • 0

@DeathLace  I dont need it to be exact. I just need it to fall into the month. Can you make it 182.5 days ahead, if that's even possible. if not at least 182, and I think that would make it fall withing the month, which is all the accuracy we need.

 

We even omit the day on the print out report and just list them as "JAN 2015" etc etc

Link to comment
Share on other sites

  • 0

It's a very bad idea to do this with JavaScript... (Users can do whatever they want with the field input value).

 

Instead do this server side with php, asp, jsp etc. You can even di this in the sql query itself.

Link to comment
Share on other sites

  • 0

My question is, why would a company put someone in charge of fixing javascript code if "My JavaScript knowledge is null so TIA." Even when given guidance, you responded asking that person to just input it into the code and make it work. I searched the code through google and this website had someone asking in June for the same thing? http://forums.caspio.com/index.php/topic/5058-expiration-date/ This feels off to me, and a very strange request for someone to ask for.

Link to comment
Share on other sites

This topic is now closed to further replies.