Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I have a scenario where I have to enter a date in a text box in MM/DD/YYYY format and there is another text box where the value should be filled by itself and the value must be exactly one month from the entered date i.e, if my value in the first text box is 05/07/2012 second text box should automatically possess 05/08/2012 and this should be done by javascript, below is the code I tried but the second text box is taking the same value as the first text box, can someone please help me out with this issue and tell me what's wrong with the code

JavaScript
function populateArchiveDate() {
var frm=document.form1
 
if (validateDateFormat(frm.story_intro_date) && frm.story_exp_date.disabled == false) {
 
var post_date = new Date(frm.story_intro_date.value);
alert(post_date.getMonth());
var expiration_date = new Date(post_date.setMonth(post_date.getMonth() + 1));
alert(expiration_date.getMonth());

if (expiration_date.getMonth() == 0)
expiration_date = expiration_date.getMonth() + 1 + "/" + expiration_date.getDate() + "/" + expiration_date.getFullYear(); 
else
expiration_date = expiration_date.getMonth() + "/" + expiration_date.getDate() + "/" + expiration_date.getFullYear();

frm.story_exp_date.value = expiration_date;
frm.hdn_story_exp_date.value = expiration_date;





Thanks in advance
Posted
Updated 7-Nov-12 4:32am
v2

if my value in the first text box is 05/07/2012 second text box should automatically possess 05/08/2012

Here javascript Date method taks 05 as month and 07 as Day and Year as 2012.

so

var date1= new Date(05/07/2012);
returns Month as 04 (Here month starts from 01-11)
Day as 07
Year as 2012

so if you add +1(04+1) to date it returns 05 only.

so same value is appearing in your second textbox.
 
Share this answer
 
try this code. its working well :-)


JavaScript
<script type="text/javascript">
$(function () {
$('#date2').focus(function () {
	            var istDateVal = $("#date1").val().split("/");
	            var istDate = new Date();
	            istDate.setFullYear(istDateVal[2], istDateVal[1] - 1, istDateVal[0]);
	            istDate.setMonth(istDate.getMonth() - 1);
	            $('#date2').val(istDate.getDate() + "/" + (istDate.getMonth() + 1) + "/" + istDate.getFullYear())
	        });
});
</script> 


HTML
<div>
    <input type="text" date1=" /><br mode=" hold=" />    <input type=" text="" id="date2" />
</div>
 
Share this answer
 
function populateArchive()
{
var frm = document.form1;
var input;
var monthVal;
var monthMax = new Array(31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var top;


if (validateDateFormat(frm.story_intro_date) && frm.story_exp_date.disabled == false)
{

var post_date = new Date(frm.story_intro_date.value);
var expiration_date = new Date(post_date.setMonth(post_date.getMonth()));
var new_date = expiration_date.getDate();
var new_month = expiration_date.getMonth() + 2;
var new_year = expiration_date.getFullYear();
if (new_month == 13)
{
new_month = new_month - 12;
new_year = new_year + 1;
}
input = parseInt(new_date, 10);
monthVal = new_month;
top = monthMax[monthVal];
if (!inRange(input, 1, top))
{
expiration_date_new = new_month + "/" + top + "/" + new_year;
}
else
expiration_date_new = new_month + "/" + new_date + "/" + new_year;
frm.story_exp_date.value = expiration_date_new;
frm.hdn_story_exp_date.value = expiration_date_new;
}

}




I faced problem in getting the desired result in the second text box if I had entered 12th month in the first text box so to face that problem i have included an if block and checked whether it is in range and added an year with that and reduced 11 months from it so that it gives the result as the first month of next year :)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900