Set new date 12 months after in a form - javascript

I am very new to script and checked many answers but could not find the complete answer.
In a form, I have a start date, number of months and end date - I want to display the end date when the start date is entered - I have created this script but I must be missing something.
Here's my code:
[script type="text/javascript" src=Datejs][/js]
[script type="text/javascript" ]
window.onload = function() {
var startdateEl = document.getElementById("customFields_cf_232");
var leasetermEl = document.getElementById("customFields_cf_34");
var enddateEl = document.getElementById("customFields_cf_38");
function CalculateDate {
var enddateEl=leasetermEl.months().startdateEl;
}
var enddateEl.onblur = CalculateDate;
};
[/script]

I made this. It listens to your keystrokes live. http://jsfiddle.net/4t54J/
<input name="startDate" type="text" value="MM-DD-YYYY" />
<input name="endDate" type="text" />
var SECOND = 1000;
var MINUTE = SECOND * 60;
var HOUR = MINUTE * 60;
var DAY = HOUR * 24;
var YEAR = DAY * 365.25;
var startDateInput = 'input[name="startDate"]';
var endDateInput = 'input[name="endDate"]';
$(startDateInput).live('keypress', function (e) {
var startDate = $(startDateInput).val();
var endDate = setEndDate(startDate);
$(endDateInput).val(endDate);
});
function setEndDate(startDate) {
var date = new Date();
var parts = startDate.split('-');
if (date != '' && parts.length > 2) {
// year, month (0-based), day
date.setFullYear(parts[2], parts[0] - 1, parts[1]);
date.setTime(date.getTime() + YEAR);
var mm = date.getMonth() + 1;
mm = (mm < 10 ? '0' : '') + mm.toString();
var dd = date.getDate();
var yyyy = date.getFullYear();
return mm + "-" + dd + "-" + yyyy;
} else {
return '';
}
}

Related

how to add dates from option value in setDate

I want to calculate the difference date from my input from and the client can determine the how many days. After that it will calculate after i choose "1 days" or "2 days", and show the result of addition dates in my input with "to" as an id.
setdate didn't add correctly from my option value. I'm using getDuration to retrieve the value in option. I assume that variable can add my setDate.
<select name="duration" id="duration" class="form-control" onchange="run(this.value)">
<script>
function run(val) {
var formDuration = document.getElementById("duration");
var getDuration = formDuration.options[formDuration.selectedIndex].value;
// alert(getDuration);
document.getElementById("from").addEventListener("change", function() {
var input = new Date(this.value);
var newdate = new Date(input);
var temp = newdate.getDate();
var calculate = temp + getDuration;
newdate.setDate(calculate);
var dd = newdate.getDate();
var mm = newdate.getMonth() + 1;
var yyyy = newdate.getFullYear();
if (dd < 10) {
dd = '0' + dd;
}
if (mm < 10) {
mm = '0' + mm;
}
var someFormattedDate = mm + '/' + dd + '/' + yyyy;
document.getElementById('to').value = someFormattedDate;
});
}
(function() {
var elm = document.getElementById('duration'),
df = document.createDocumentFragment();
for (var i = 1; i <= 12; i++) {
var option = document.createElement('option');
option.value = i;
option.appendChild(document.createTextNode(i + " days"));
df.appendChild(option);
}
elm.appendChild(df);
}());
</script>
</select>
dates in this format : mm/dd/yyyy
example 1
from : 07/01/2019
duration : 1 days
to : 07/11/2019
example 2
from : 07/02/2019
duration : 1 days
to : 07/21/2019
example 3
from : 07/03/2019
duration : 1 days
to : 07/31/2019
expected results
from : 07/01/2019
duration : 1 days
to : 07/02/2019

How to Change a specific value in input using JavaScript

I have an input containing a date value
example:
<input type="text" name="cel_dafa" value="26/12/2018">
I want a button when pressed it increases the value one month
So that the result after the pressure:
<input type="text" name="cel_dafa" value="26/1/2019">
Note: I do not want to harm the day,
Only the month and year if at the end of the year
Ok, so consider you have a button like;
<button onclick="increase()"> Click Me </button>
And input like;
<input type="text" id="date" name="cel_dafa" value="26/12/2018">
Now code of increase function;
function increase()
{
var currentDate = document.getElementById('date').value;
var parts = currentDate.split("/");
var day = parts[0];
var month = parts[1];
var year = parts[2];
var d = new Date(year, month - 1, day);
d.setMonth(d.getMonth() + 1);
var newDate = d.getDate() + "/" + (d.getMonth() + 1) + "/" + d.getFullYear();
document.getElementById('date').value = newDate;
}
Html
<input type="text" id="date-input" name="cel_dafa" value="26/12/2018">
<button onclick="increaseDate()">submit</button>
JS
function increaseDate() {
var dateInput = document.querySelector('#date-input').value;
var day = dateInput.split('/')[0];
var month = dateInput.split('/')[1];
var year = dateInput.split('/')[2];
if (month > 11) {
year = parseInt(year) + 1;
month = 1
} else {
month = parseInt(month) + 1;
}
var newDate = day +"/"+ month +"/"+ year;
document.querySelector('#date-input').value = newDate;
}

Age restriction using Javascript

I am new to JavaScript with absolutely little idea about the language. I am trying to put an age restriction in a job application form where for the date of birth text field, date format is dd/mm/yyyy and applicants must be at between 15 and 80 years old at the time they fill in the form otherwise they won't be able to apply. I do not want to embed it into HTML file but write it in .js file only.
for DOB input type is text, name is dob, id is dob, pattern is (0[1-9]|1[0-9]|2[0-9]|3[01])/(0[1-9]|1[012])/[0-9]{4}
Thank you.
You can use min and max attributes of HTML5 input date
HTML:
<input type="date" id="txtDate" />
JavaScript :
var dtToday = new Date();
var month = dtToday.getMonth() + 1;
var day = dtToday.getDate();
var year = dtToday.getFullYear();
var maxYear = year - 18;
if(month < 10)
month = '0' + month.toString();
if(day < 10)
day = '0' + day.toString();
var maxDate = maxYear + '-' + month + '-' + day;
var minYear = year - 80;
var minDate = minYear + '-' + month + '-' + day;
alert(maxDate);
document.querySelectorAll("#txtDate")[0].setAttribute("max",maxDate);
document.querySelectorAll("#txtDate")[0].setAttribute("min",minDate);
function processDate(date){
var parts = date.split("/");
return new Date(parts[2], parts[1] - 1, parts[0]);
}
function calcAge(date) {
var dBirth = processDate(date);
var dToday = new Date();
var diff = dToday.getTime() - dBirth.getTime();
return Math.floor(diff / (1000 * 60 * 60 * 24 * 365.25));
}
function validateDate(date){
var age = calcAge(date);
console.log(age);
if(15<=age && age <=80) return true;
else {
return false;
}
}
console.log(validateDate("01/12/1988"));
console.log(validateDate("02/11/1911"));

Add day(s) in a date in Javascript

I have textbox displaying date and have a button. the function in button is to add 7 days and display in textbox. my code:
function onNext() {
var startdate = document.getElementById('date').value;
var addday = new Date(startdate);
var dd = addday.getDate() + 7;
var mm = addday.getMonth() + 1;
var y = addday.getFullYear();
var displaydate = y + '/' + mm + '/' + dd;
document.getElementById('date').value = displaydate ;
}
The issue how to add a day to go to the next month.
Example the date in Textbox is 2014/08/25 when I click the button the date will be 2014/09/01
Just add 7 days to your date, date already handles change of month/year:
function onNext() {
var startdate = document.getElementById('date').value;
var addday = new Date(startdate);
addday.setDate(addday.getDate() + 7);
var dd = addday.getDate() + 7;
var mm = addday.getMonth() + 1;
var y = addday.getFullYear();
var displaydate = y + '/' + mm + '/' + dd;
document.getElementById('date').value = displaydate ;
}
If you just do this
var dd = addday.getDate() + 7;
var mm = addday.getMonth() + 1;
var y = addday.getFullYear();
that means if date is 21.12.2014 the output will be 28.13.2014
function onNext() {
var startdate = document.getElementById('date').value;
var d2 = new Date(startdate);
d2.setMonth(d2.getMonth()+1);
d2.setDate(1); // you can set here whatever date you want
document.getElementById('date').value = d2.getFullYear() + '/' + d2.getMonth() + '/' + d2. getDate();
}
Use this function
function updateAb(s){//format dd/mm/yyyy chnage according to your need
var dmy = s.split("/");
var joindate = new Date(
parseInt(dmy[2], 10),
parseInt(dmy[1], 10) - 1,
parseInt(dmy[0], 10)
);
var data_days=7;
joindate.setDate(joindate.getDate() + data_days);
var cc=("0" + joindate.getDate()).slice(-2) + "/" +("0" + (joindate.getMonth() + 1)).slice(-2) + "/" +joindate.getFullYear();
document.getElementById("datepickerdisabled1").value=cc;
}

Compare two dates in JS

I want to compare the user's birthday against today's date and get the number of days in between. The birthday they enter will be in the form of 12/02/1987 in an input box of type text
In my JS file I have code that looks like this:
function validateDOB(element) {
var valid = false;
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1; //do that January is NOT represented by 0!
var yyyy = today.getFullYear();
if (dd < 10) {
dd = '0' + dd
}
if (mm < 10) {
mm = '0' + mm
}
var today = mm + '/' + dd + '/' + yyyy;
alert(today);
if (element.value != today) {
var days = 0;
var difference = 0;
Christmas = new Date("December 25, 2011");
today = new Date();
difference = today - Christmas
days = Math.round(difference / (1000 * 60 * 60 * 24)-1);
alert(days);
valid = true;
}
Instead of using "Christmas" I want to compare element.value... how do I do this?
When I put difference = today - element.value it won't show me the difference. The alert box comes up as NaN.
I wrote a lightweight date library called Moment.js to handle stuff like this.
var birthday = moment('12/02/1987', 'MM-DD-YYYY');
var inputDate = moment(element.value, 'MM-DD-YYYY');
var diff = birthday.diff(inputDate, 'days');
http://momentjs.com/docs/#/displaying/difference/
You'll need to first parse element.value as a date:
difference = today - new Date(element.value);
http://jsfiddle.net/gilly3/3DKfy/

Categories

Resources