Web Analytics Made Easy -
StatCounter Javascript DateFormat ( 6 month window) - CodingForum

Announcement

Collapse
No announcement yet.

Javascript DateFormat ( 6 month window)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Javascript DateFormat ( 6 month window)

    Hi Guys ,

    i am wondering how i can get the current 6 month windows like the following

    always based onthe current month


    Aug-11
    Sep-11
    Oct-11
    Nov-11
    Dec-11
    Jan-12


    thanks!

  • #2
    PHP Code:
    var monthSpan 6;
    var 
    months = ['Jan''Feb''Mar''Apr''May''Jun''Jul''Aug''Sep''Oct''Nov''Dec'];

    var 
    date = new Date();
    date.setDate(1);

    var 
    output "";
    for (var 
    0monthSpani++) {
        
    output += months[date.getMonth()] + '-' + (date.getFullYear() % 100) + '\n';
        
    date.setMonth(date.getMonth() + 1);
    }

    alert(output); 
    .My new Javascript tutorial site: http://reallifejs.com/
    .Latest article: Calculators — Tiny jQuery calculator, Full-fledged OOP calculator, Big number calculator
    .Latest quick-bit: Including jQuery — Environment-aware minification and CDNs with local fallback

    Comment


    • #3
      Thank you so much venegal for the help ,

      i will figure out how to assign each month a specific variable...

      thanks again

      Comment


      • #4
        Do you mind another question. i am stack on something else ..
        iam unable to get the value of a dynamic generated dropdownlist

        i can get it on firefox but not IE..

        any idea ?

        thanks

        Comment


        • #5
          For this one, you should probably post your code, so we can see why your method wouldn't work in IE.
          Last edited by venegal; Aug 17, 2011, 12:28 PM. Reason: typo
          .My new Javascript tutorial site: http://reallifejs.com/
          .Latest article: Calculators — Tiny jQuery calculator, Full-fledged OOP calculator, Big number calculator
          .Latest quick-bit: Including jQuery — Environment-aware minification and CDNs with local fallback

          Comment


          • #6
            OK i ll do ,
            thanks budd. i will create a new thread and hope you will see it..lol

            Comment


            • #7
              Originally posted by korssane View Post
              i will figure out how to assign each month a specific variable...
              Don't do it!

              Use an array and assign the months to elements of the array.

              Example, based on Venegal's code:
              Code:
              var monthSpan = 6;
              var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
              
              var date = new Date();
              date.setDate(1);
              
              [COLOR="Red"]var namedMonths = []; // this is the array of names![/COLOR]
              for (var i = 0; i < monthSpan; i++) {
                  [COLOR="Red"]namedMonts[i] = months[date.getMonth()] + '-' + (date.getFullYear() % 100);[/COLOR]
                  date.setMonth(date.getMonth() + 1);
              }
              Now you have a nice array, where namedMonths[0] is the current month and (for example) namedMonths[5] is the sixth month.

              MUCH better than trying to create and use 6 separate variables.
              Be yourself. No one else is as qualified.

              Comment


              • #8
                already did it and it works great

                thanks a bunch guys.

                Comment


                • #9
                  Hi Guys,
                  i got another issue when trying to insert those values into oracle db ..
                  it seems it does not recognize mmm-yy format

                  and gives me ORA-01843: not valid month
                  my Oracle field is a date filed type.

                  Is there a way to leave the actual format and just create another one with date type ?
                  or just give it the 'mm/dd/yyyy' format with mask 'mmm-yy'

                  thanks
                  Last edited by korssane; Aug 19, 2011, 10:58 AM.

                  Comment

                  Working...
                  X
                  😀
                  🥰
                  🤢
                  😎
                  😡
                  👍
                  👎