使用javasript在输入的日期中添加一个月 [英] Adding A month to the entered date using javasript

查看:103
本文介绍了使用javasript在输入的日期中添加一个月的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一种情况,我必须在MM/DD/YYYY格式的文本框中输入日期,并且还有另一个文本框,其中值应自己填写,并且值必须距输入日期正好一个月,即如果我在第一个文本框中的值是05/07/2012第二个文本框应自动拥有2012年5月8日,并且这应该由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

推荐答案

如果我在第一个文本框中的值是05/07/2012第二个文本框应自动拥有2012年5月8日

这里的javascript Date方法将05表示为月,将07表示为Day and Year作为2012.

所以

var date1 = new Date(05/07/2012);
返回月份为04(这里的月份从01-11开始)
一天作为07
2012年

因此,如果您在日期上加上+1(04 + 1),则只会返回05.

因此相同的值会出现在您的第二个文本框中.
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.


尝试此代码.它运作良好:-)


try this code. its working well :-)


<script type="text/javascript">


(函数(){


这篇关于使用javasript在输入的日期中添加一个月的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆