javascript - Add 30 days to date (mmddyyyy) then return string (mmddyyyy) - Stack Overflow

I have the function below which takes a string from the datepicker and turns it into a date object so I

I have the function below which takes a string from the datepicker and turns it into a date object so I can add 30 days to it. From there I am trying to return the new date as a string with 30 days added to it, in the format of (mm/dd/yy).

When the first alert fires it correctly adds 30 days to the selected date and shows this for selcted date as "05/03/2011":

Thu Jun 02 2011 00:00:00 GMT+0100 (GMT Daylight Time)

The second alert shows

5/2/2011

Seems I can't correctly format the date and take "05/03/2011" and return "06/02/2011". I could just do month + 1, but could do with some help please and show me what I am doing wrong.

  $('#sign_date').datepicker({
      onSelect: function(dateText, inst) { 

          var d = new Date(dateText);
          d.setDate(d.getDate() + 30);

          alert(d);

          var date = d.getDate();
          var month = d.getMonth();
          var year = d.getFullYear();

          alert(month+'/'+date +'/'+year)

      }
  });

Also I think they way I am doing it will show days and month as e.g. Jan = 1 and 1st = 1 and I would like it to be Jan = 01 and 1st = 01

Thanks

I have the function below which takes a string from the datepicker and turns it into a date object so I can add 30 days to it. From there I am trying to return the new date as a string with 30 days added to it, in the format of (mm/dd/yy).

When the first alert fires it correctly adds 30 days to the selected date and shows this for selcted date as "05/03/2011":

Thu Jun 02 2011 00:00:00 GMT+0100 (GMT Daylight Time)

The second alert shows

5/2/2011

Seems I can't correctly format the date and take "05/03/2011" and return "06/02/2011". I could just do month + 1, but could do with some help please and show me what I am doing wrong.

  $('#sign_date').datepicker({
      onSelect: function(dateText, inst) { 

          var d = new Date(dateText);
          d.setDate(d.getDate() + 30);

          alert(d);

          var date = d.getDate();
          var month = d.getMonth();
          var year = d.getFullYear();

          alert(month+'/'+date +'/'+year)

      }
  });

Also I think they way I am doing it will show days and month as e.g. Jan = 1 and 1st = 1 and I would like it to be Jan = 01 and 1st = 01

Thanks

Share Improve this question edited Nov 27, 2011 at 23:42 user212218 asked May 9, 2011 at 2:42 JimJim 2271 gold badge3 silver badges6 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

For leading zeros:

// add leading zero if the length equals 1
if (month < 10) month = "0" + month;
if (day < 10)   day   = "0" + day;

Be sure to add 1 to your month prior to using this code, too, since getMonth() returns a 0 for January, and so on:

 var month = d.getMonth() + 1;

Surprise, surprise... The getMonth() method returns the month in the range 0..11.

Answer from Kelly works to me

Just changed this

if(month<9) month = "0"+(month+1);
if(date<10) day = "0"+date;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745178127a4615292.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信