Jump to content

Adding One Year to a Date Field?


firstplacedave

Recommended Posts

Hello,

I'm a bit desperate here. We are rolling out a new EHR (Electronic Health Record) next year and it supports Java. We've already done a few things like having it pull in the date, however for some of the forms we would like it to populate it one year from today's date (whatever the date is.) Here is what we have for an example. This field automatically pulls it in and disables a calendar button so it can't be changed. What we would like, is if we could have it display 11/11/2016, etc.

Code: todaysDateTime()

Photo attached of what it currently looks like:

 

Any help you can provide me would be awesome.

post-191619-0-65818000-1447269792_thumb.png

Link to comment
Share on other sites

What have you got so far? I would start with looking at the JavaScript date object

http://www.w3schools.com/jsref/jsref_obj_date.asp

 

There's also a really good date / time library called moment that is helpful when working with date / times

http://momentjs.com/

  • Like 1
Link to comment
Share on other sites

What have you got so far? I would start with looking at the JavaScript date object

http://www.w3schools.com/jsref/jsref_obj_date.asp

 

There's also a really good date / time library called moment that is helpful when working with date / times

http://momentjs.com/

 

I haven't really gotten anywhere. I've been searching the web and I have gotten some ideas and have tried them, and they do not work exactly how is needed. One seemed like it was going to display properly, however it just displayed "Fri" which was correct, November 11th would be a Friday next year, but we need it in a MM/DD/YYYY format. Another one that I tried gave me '2016' in the display box, which again is close but not exact. The code for that is:

var myDate = new Date();

alert(myDate.getYear() + 1);

 

I don't think this will work as I don't see any formatting.

Edited by firstplacedave
Link to comment
Share on other sites

It's hard to help you without seeing any code or what you tried and why a given approach isn't working.

 

Formatting date / time is extremely common and not that hard, and in particular what moment excels at. Did you review their docs? There's a section just on displaying.

http://momentjs.com/docs/#/displaying/

  • Like 1
Link to comment
Share on other sites

set one year in advance

var myDate = new Date();
myDate.setFullYear(myDate.getYear() + 1);

Then retrieve individual parts of this new set date, and separate them with "/".

Remember JavaScript month begins with 0, also you need function to check if individual date and month values are below 10, if they are place "0" at beginning.

  • Like 1
Link to comment
Share on other sites

Because the format of "11/12/2015" is not the format it uses to gather the correct date, see last example here http://www.w3schools.com/jsref/jsref_getdate.asp

 

You are looking for something similar to this

<!DOCTYPE html>
<html>
<body>

<p>Click the button to display todays day of the month.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
   var myDate = new Date();

var weekdayArray =["Sun","Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];

//myDate.setFullYear(myDate.getFullYear() + 1); //use current date and increase year by 1

myDate.setFullYear(1983, 4, 6); //second month parameter always start at 0 (so 4 = may)

var day = weekdayArray[myDate.getDay()];
var date= zerothis(myDate.getDate());
var month= zerothis(myDate.getMonth()+1); //brings to correct month value
var year= myDate.getFullYear();

var join = day+ " "+date+"/"+month+"/"+year;

    document.getElementById("demo").innerHTML = join;
}

function zerothis(val)
{
if(parseInt(val)<10)
{
val = "0"+val;
}
return val;
}
</script>

</body>
</html>
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...