Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 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
    Regular Coder
    Join Date
    Aug 2010
    Posts
    967
    Thanks
    19
    Thanked 211 Times in 209 Posts
    var d=new Date();
    var datestring=d.getDate() +" "+ d.getMonth() +" "+ d.getYear()
    alert(datestring)


    Don't forget about Holidays

  • Users who have thanked DaveyErwin for this post:

    brock029 (09-06-2011)

  • #3
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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; 09-06-2011 at 02:05 AM.

  • #4
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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?

  • #5
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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; 09-06-2011 at 02:37 AM.

  • #6
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,078
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb 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>

  • Users who have thanked jmrker for this post:

    brock029 (09-06-2011)

  • #7
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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?

  • #8
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,078
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb

    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.

  • #9
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,078
    Thanks
    38
    Thanked 498 Times in 492 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •