Web Analytics Made Easy -
StatCounter Displaying a Date with another format - CodingForum

Announcement

Collapse
No announcement yet.

Displaying a Date with another format

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

  • Displaying a Date with another format

    Alright so I've been searching around trying to figure out how to change the date format, the way I have seen it the most is like

    Code:
    var d=new Date();
    var datestring=d.getdate + d.getMonth + d.get Year
    I am new to javascript and this is the first script I have written on my own so this is what it does. It is supposed to display the date that a shipment should arrive on with 2 day shipping through ups so it skips weekends. It displays the date after either adding 2 days or more depending on the day of the week. But the way the date is formatted really needs to be changed. So if any one can suggest maybe how to tie in the above code with mine that would be great.

    Code:
    <script type="text/javascript">
    var d=new Date();
    var day=d.getDay();
    if (day <= 3){
        d.setDate (d.getDate() + 2);
    }
    else if (day == 4) {
        d.setDate (d.getDate() + 4)
    }
    else if (day == 5){
        d.setDate (d.getDate() + 4);
    }
    else if (day == 6) {
        d.setDate (d.getDate() + 3);
    }
    document.write("Your card should arrive on " + d);
    </script>

  • #2
    var d=new Date();
    var datestring=d.getDate() +" "+ d.getMonth() +" "+ d.getYear()
    alert(datestring)


    Don't forget about Holidays

    Comment


    • #3
      Yeah Holidays is the next step but first I have to figure out how to lol. So my next question is how do i tie
      Code:
      var datestring=d.getDate() +" "+ d.getMonth() +" "+ d.getYear()
      alert(datestring)
      into

      Code:
      <script type="text/javascript">
      var d=new Date();
      var day=d.getDay();
      if (day <= 3){
          d.setDate (d.getDate() + 2);
      }
      else if (day == 4) {
          d.setDate (d.getDate() + 4)
      }
      else if (day == 5){
          d.setDate (d.getDate() + 4);
      }
      else if (day == 6) {
          d.setDate (d.getDate() + 3);
      }
      document.write("Your card should arrive on " + d);
      </script>
      To make it show the correct date that its adding on days for....for example if you load the scirpt up right now it shows "Your card should arrive on Wed Sep 7 19:14:46 CDT 2011" and if i just add in the var datestring it gives me "Your card should arrive on 5 8 2011 " which is the wrong month and day. I think I messed up somewhere.

      EDIT:
      Its getting the correct day just the wrong month.
      Last edited by brock029; Sep 5, 2011, 09:05 PM.

      Comment


      • #4
        Ok so I figured out one part of my problem get month works from 0-11 not 1-12 so is there a way to change that?

        Comment


        • #5
          Ok fixed it and got it working pretty good it now displays "Your card should arrive on Wednesday,September 7,2011"

          Code:
          <script type="text/javascript">
          var d=new Date();
          
          //var for counting days
          var day=d.getDay();
          
          //array for naming the days
          var weekday=new Array(7);
          weekday[0]="Sunday";
          weekday[1]="Monday";
          weekday[2]="Tuesday";
          weekday[3]="Wednesday";
          weekday[4]="Thursday";
          weekday[5]="Friday";
          weekday[6]="Saturday";
          
          //array for naming the months
          var month=new Array(12);
          month[0]="January";
          month[1]="February";
          month[2]="March";
          month[3]="April";
          month[4]="May";
          month[5]="June";
          month[6]="July";
          month[7]="August";
          month[8]="September";
          month[9]="October";
          month[10]="November";
          month[11]="December";
          
          //statements to do the math of adding two business days 
          if (day <= 3){
              d.setDate (d.getDate() + 2);
          }
          else if (day == 4) {
              d.setDate (d.getDate() + 4)
          }
          else if (day == 5){
              d.setDate (d.getDate() + 4);
          }
          else if (day == 6) {
              d.setDate (d.getDate() + 3);
          }
          
          //var to display the date better
          var datestring=month[d.getMonth()] +" "+d.getDate() +","+ d.getYear();
          
          //output to display the date
          document.write("<b><i>Your card <font color=red>should</font> arrive on</i></b> " + weekday[d.getDay()] +","+ datestring);
          </script>
          Last edited by brock029; Sep 5, 2011, 09:37 PM.

          Comment


          • #6
            Simplify???

            If I'm understanding what you want to do,
            you could simplify it like this...
            Code:
            <!DOCTYPE HTML>
            <html>
            <head>
            <title> Untitled </title>
            <script type="text/javascript">
            // From: 
            
            var d=new Date();
            var day=d.getDay();
            var weekday=['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
            
            var month=['January','February','March','April','May','June',
                       'July','August','September','October','November','December'];
            var d2a = 0;
            switch(day) {
              case 4: d2a = 4; break;  // Thursday
              case 5: d2a = 4; break;  // Friday
              case 6: d2a = 3; break;  // Saturday
             default: d2a = 2; break;  // all other days
            }
            d.setDate(d.getDate()+d2a);
            
            var datestring=month[d.getMonth()] +" "+d.getDate() +","+ d.getFullYear();
            var str = "<b><i>Your card <font color=red>should</font> arrive on</i></b> "
                    + weekday[d.getDay()] +", "+ datestring;
            document.write(str);
            </script>
            
            </head>
            <body>
            
            </body>
            </html>

            Comment


            • #7
              Thats exactly it, does the same thing I wrote just alot cleaner...now to add holidays in the mix and it would be perfect! But one thing what exactly does the

              Code:
              switch(day) {
                case 4: d2a = 4; break;  // Thursday
                case 5: d2a = 4; break;  // Friday
                case 6: d2a = 3; break;  // Saturday
               default: d2a = 2; break;  // all other days
              }
              part of it do....or how does it work?

              Comment


              • #8
                The "switch" checks the value of 'day', which ranges from 0...6
                In the case the day is 4, it assigns 4 to the variable 'd2a'
                If the day is 0, 1, 2 or 3, the default assignment is 2.

                The assignment is added to the datestring display you already had coded.

                See here for more explanations about the 'switch' statement.
                or do a 'google' search on 'javascript switch' for other sites.

                Comment


                • #9
                  You can check for holidays with javascript using one of the following functions:
                  See: http://www.softcomplex.com/forum/viewthread_2814/
                  or: http://www.hotscripts.com/listing/federal-holiday/

                  Probably the easiest way would be to form the expected delivery date as you already do
                  and then check it against a list of holiday dates set-up in an array for the year's dates
                  If a match is found then add in an extra day (??? or more) before you make the final display.

                  Comment

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