Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET Javascript
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

 
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 7-Nov-12 0:52am
Edited 7-Nov-12 5:32am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

try this code. its working well Smile | :)
 

<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> 
 
<div>
    <input type="text" date1=" /><br mode=" hold=" />    <input type=" text="" id="date2" />
</div>
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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 Smile | :)
  Permalink  
v2

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

  Print Answers RSS
0 Peter Leow 293
1 Kornfeld Eliyahu Peter 220
2 Sergey Alexandrovich Kryukov 162
3 Maciej Los 160
4 OriginalGriff 153
0 OriginalGriff 383
1 Peter Leow 318
2 Kornfeld Eliyahu Peter 260
3 Maciej Los 230
4 Sergey Alexandrovich Kryukov 212


Advertise | Privacy | Mobile
Web03 | 2.8.150301.1 | Last Updated 9 Nov 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100