|
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
|
|
|
|
|
SadiqMohammed wrote: 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();
There's your problem - in Javascript, months run from 0 to 11, not 1 to 12. If the new date is in January, you're setting the result correctly; otherwise, you're setting it to the previous month.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
@Richard : I changed it and considering that Javascript takes month index as 0 to 11 but now when I enter the date as 01/31/2012 the value in the next text box is 04/01/2012 but for rest of the months I'm getting the desired solution
All Izzz Wellll
|
|
|
|
|
I don't know how you've managed to get 1st April; with the code below, I get 2nd March:
var post_date = new Date(2012, 0, 31);
var expiration_date = new Date(post_date.setMonth(post_date.getMonth() + 1));
expiration_date = expiration_date.getMonth() + 1 + "/" + expiration_date.getDate() + "/" + expiration_date.getFullYear();
alert(expiration_date);
The reason it's 2nd March is that there is no 31st February. If you want the expiration_date to always be a date within the following month, you'll need to check that manually:
var post_date = new Date(2012, 0, 31);
var firstOfMonth = new Date(post_date);
firstOfMonth.setDate(1);
firstOfMonth.setMonth(post_date.getMonth() + 1);
var expiration_date = new Date(firstOfMonth);
expiration_date.setDate(post_date.getDate());
while (expiration_date.getMonth() != firstOfMonth.getMonth())
{
expiration_date.setDate(expiration_date.getDate() - 1);
}
expiration_date = expiration_date.getMonth() + 1 + "/" + expiration_date.getDate() + "/" + expiration_date.getFullYear();
alert(expiration_date);
Alternatively, you could look at the Datejs library[^].
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi Richard,
Thanks for the suggestion I solved the problem and I'm getting the desired result, I faced a problem only for the month of November i.e, 11th month but I resolved it using the following code with the help of a if block
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;
}
}
thanks for ur suggestions once again
|
|
|
|
|
try this code its working well.
$(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())
});
});
|
|
|
|
|
... and in December, what happens?
Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
I didnt get you bro. what is your concern. Will you elaborate it.
deepak.m.shrma
|
|
|
|