Increase Month by one in javascript

Last post 05-12-2008 10:09 PM by sivakl_2001. 10 replies.

Sort Posts:

  • Increase Month by one in javascript

    05-10-2008, 4:40 AM

    Hi
    i am showing date in a textbox like APR-2008 in MMM-YYYY fromat. and there is a buttun next onclick next it will increase month by 1 like MAY-2008. This is working fine and code for that is  


    Protected Sub btnNext_Click

            txtDatSalDate.Text = DateAdd("m", 1, CDate(txtDatSalDate.Text))
            txtDatSalDate.Text = CDate(txtDatSalDate.Text).ToString("MMM-yyyy")

    end sub

    but the problem is of page postback
    i want to do it with javascript

  • Re: Increase Month by one in javascript

    05-10-2008, 6:40 AM
    please help
  • Re: Increase Month by one in javascript

    05-10-2008, 2:31 PM
    • Member
      59 point Member
    • dits59
    • Member since 06-25-2006, 5:54 PM
    • Cochin
    • Posts 25

    check this.well this might work or not..i used this js for formatting.,.if u  need  u can create ur own..

    <script type='text/javascript' src="http://stevenlevithan.com/assets/misc/date.format.js">
    </script>
    </head>
    <body>

    <input type="text" id="txt" value=""/>
    <a href="javascript:void(0);" onclick="increment();">Increment Month</a>

    </body>


    <script type='text/javascript'>


    var today = new Date();
    document.getElementById("txt").value=today.format("mmm-yyyy");

    var months = new makeArray('January','February','March','April',
                               'May','June','July','August','September',
                               'October','November','December');

    function y2k(number) { return (number < 1000) ? number + 1900 : number; }


    function monthsahead(noofmonths) {

    tempdate=today;
    var tempdate= new Date(y2k(today.getYear()),today.getMonth() + noofmonths,today.getDate(),today.getHours(),today.getMinutes(),today.getSeconds());
    today=tempdate;
    return tempdate;
    }


    function makeArray()
    {
        for (i = 0; i<makeArray.arguments.length; i++)
           this[i + 1] = makeArray.arguments[i];
    }


    function increment()
    {
    document.getElementById("txt").value=monthsahead(1).format("mmm-yyyy");;
    }


    </script>

     i think u can easily achieve this by using some js libraries.

    References http://www.irt.org,http://stevenlevithan.com/

  • Re: Increase Month by one in javascript

    05-10-2008, 3:12 PM
    • Participant
      1,254 point Participant
    • nmgomes
    • Member since 04-09-2007, 11:53 PM
    • Portugal
    • Posts 217

    Hi,

    You should use the Date object from JScript.

    With Date object you can parse a string to date and add/subtract dates. What you don't have is a direct conversion from month as int to month as name, this case you must convert the int to the correct month name.

    Note: when using this object keep always in mind that month int is range fom 0 to 11 and not as usual 1 to 12.

     

    Nuno Gomes [visit my blog]
    Portugal - Europe's West Coast
    [Don't forget to click "Mark as Answer" on the post(s) that helped you.]
  • Re: Increase Month by one in javascript

    05-12-2008, 2:44 AM

    Hi dits

    it is giving error in

    document.getElementById("txt").value=today.format("mmm-yyyy");

  • Re: Increase Month by one in javascript

    05-12-2008, 2:46 AM

    Hi nmgomes,

    can you do this for me.

    javascript always put trouble for me.

  • Re: Increase Month by one in javascript

    05-12-2008, 3:50 AM
    Answer
    • Contributor
      2,778 point Contributor
    • sivakl_2001
    • Member since 01-11-2008, 3:13 AM
    • Kuala Lumpur, Phileo Damansara
    • Posts 804

    HI harminder try this javascript function

     

    function check()

    {

     

    dat = document.getElementById(
    "TextBox3").value;

    y = dat.substr(4,4);

    dat = dat.substr(0,3);

    var months = new Array("JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC");

    for(i=0;i<months.length;i++)

    {

    if(i==11)

    {

    dat=months[0];

    break;

    }

    if(months[i]==dat)

    {

    dat = months[i+1]

    break;

    }

    }

     

    document.getElementById(
    "TextBox3").value = dat +"-"+ y;

     

     

    }

  • Re: Increase Month by one in javascript

    05-12-2008, 4:18 AM
    • Member
      59 point Member
    • dits59
    • Member since 06-25-2006, 5:54 PM
    • Cochin
    • Posts 25

     i dunno find any error.

    check this one http://examples.gohsphere.com/js_date.html

    i think  sivakl_2001 soln is better than myself..

    u can customize it for the year too,with just one line of code..

  • Re: Increase Month by one in javascript

    05-12-2008, 7:59 AM

    Hi babaji

     this is working perfactly
    but when i am calling this function on another button on which month
    should decrease in place of increase . it is not working
    what i have change is
    dat = months[i-1]

  • Re: Increase Month by one in javascript

    05-12-2008, 8:08 AM

    hi,

    sivalk(Babaji) it is working perfactly.

    the problem is i am not getting is of upper and lower case

    thanks

  • Re: Increase Month by one in javascript

    05-12-2008, 10:09 PM
    • Contributor
      2,778 point Contributor
    • sivakl_2001
    • Member since 01-11-2008, 3:13 AM
    • Kuala Lumpur, Phileo Damansara
    • Posts 804

    hi harminder try this

    function check()

    {

     

    dat = document.getElementById(
    "TextBox1").value;

    y = dat.substr(4,4);

    dat = dat.substr(0,3);

    dat = dat.toUpperCase();

     

     

    var months = new Array("JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC");

    for(i=0;i<months.length;i++)

    {

    if(i==11)

    {

    var year = parseInt(y);

    year = year +1;

    y = year.toString();

    dat=months[0];

    break;

    }

    if(months[i]==dat)

    {

    dat = months[i+1];

    break;

    }

    }

     

    document.getElementById(
    "TextBox1").value = dat +"-"+ y;

     

     

    }

     

Page 1 of 1 (11 items)