www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: Help with Showing Dates in Html Pages

  1. #1
    Join Date
    May 2013
    Posts
    8

    Unhappy Help with Showing Dates in Html Pages

    Hi, can you help me achieve this please, been searching for hours with no luck

    I would like to show current date with 4 days added to it excluding weekends.

    I can do without the weekend if not possible

    e.g Order Today 13/05/2013 and receive your order 17/05/2013

    Any help is much appreciated
    Thanks
    William

  2. #2
    Join Date
    Sep 2007
    Location
    istanbul
    Posts
    317
    If I understand
    Code:
      
    
    <script type="text/javascript">
    // http://www.webdeveloper.com/forum/showthread.php?277859-Help-with-Showing-Dates-in-Html-Pages
    
    // http://www.w3schools.com/js/js_obj_date.asp
    
    // http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_date_weekday
    
    var d = new Date('2013 05 13' );
    
    // alert(d);
    // alert(d.getDay());
    // alert(d.getDay() + 6);
    // alert( (d.getDay() + 6)%7);
    
    var i;
    var n = 4;
    var s;
    
    for( i= 1; i <=  4; i++){
    s = (d.getDay() + i) % 7 ;
    if( s== 6 || s == 0) { n=6; break; }
    }
    alert(n);
    
    d.setDate(d.getDate() + n);
    
    alert(d);
    
    </script>

  3. #3
    Join Date
    May 2013
    Posts
    8
    Thanks for your help, I will give it a try and let you know how I get on,

    William

  4. #4
    Join Date
    May 2013
    Posts
    8
    I just tried that and its shows a message with the number "4" followed by the message "Invalid Date"

    Any help on this please?

    Just to clarify will this print the date into the html page or show as a pop window? Its currently showing those messages as popup window, would like it printed on to the page if possible

  5. #5
    Join Date
    May 2013
    Posts
    8

  6. #6
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,270
    Try simply this method :
    Code:
    var tdy=new Date(),gap=0;
    // We add one day while the gap is lower to 4 or the day is a saturday or sunday 
    do {tdy.setDate(tdy.getDate()+1)} while(++gap<4 || tdy.getDay()==6  || tdy.getDay()==0);
    
    // To test the method  
     for (i=1;i<7;i++) {
         tdy=new Date();gap=0;
         tdy.setDate(tdy.getDate()+i);
         var first=tdy.toLocaleString();
         do {tdy.setDate(tdy.getDate()+1)} while(++gap<4  || tdy.getDay()==6  || tdy.getDay()==0);
         alert('first date : '+first+'\nsecond : '+tdy.toLocaleString())
     }
    it's possible to resume the two conditions : tdy.getDay()==6 || tdy.getDay()==0
    with the unique condition : (1+tdy.getDay())%7<2
    Last edited by 007Julien; 05-13-2013 at 08:28 AM. Reason: Errors...

  7. #7
    Join Date
    May 2013
    Posts
    8
    hi, just tried that to, shows a syntax error?

    Am i right to put it in like so

    <script>

    var tdy=new Date(),gap=0;
    // We add one day while the gap is lower to 4 or the day is a saturday or sunday
    do {tdy.setDate(tdy.getDate()+1)} while(++gap<4 || (2<(1+tdy.getDate())%7);

    // To test the method
    for (i=1;i<7;i++) {
    tdy=new Date();gap=0;
    tdy.setDate(tdy.getDate()+i);
    var first=tdy.toLocaleString();
    do {tdy.setDate(tdy.getDate()+1)} while(++gap<4 || 2<(1+tdy.getDate())%7);
    alert('first date : '+first+'\nsecond : '+tdy.toLocaleString())
    }

    </script>


    Thanks

  8. #8
    Join Date
    Sep 2007
    Location
    istanbul
    Posts
    317
    Code:
      
    <script type="text/javascript">
    // http://www.webdeveloper.com/forum/showthread.php?277859-Help-with-Showing-Dates-in-Html-Pages
    
    // http://www.w3schools.com/js/js_obj_date.asp
    
    // http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_date_weekday
    
    //var d = new Date(2013, 05-1, 18 ); 
    
    var d = new Date();
    
    alert(d);
    
    var i, n, count=0,  day= d.getDay();
    
    // alert(day);
    
    for(i=1; ; i++) {
    
       if ( ((1 + day + i )%7) > 1  ) {  count++;  }
    
    //  n = (day + i) %7 ;
    //   if( n != 0  && n != 6 )   {   count++;  }
    
       if(count == 4) { break;}
    }
    // alert('count = '+count + '\n  i = '+i);
    
    var tarih = new Date(d.setDate(d.getDate() + i));
    
    alert(tarih);
    
    
    </script>
    Last edited by Ayşe; 05-13-2013 at 01:02 PM.

  9. #9
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,270
    The condition is while(++nmb<4 || (1+tdy.getDay())%7<2; !
    Saturday : ((6+1)%7=>0 Sunday => ((0+1)%7=>1 . Then your have to add one with this two cases

  10. #10
    Join Date
    May 2013
    Posts
    8
    hi guys, thanks again, i think we've made some progress, not quite right yet thou, you may want to see the page link below,

    Its shows it as a pop up window, i have highlighted where i would like the info to be shown if possible

    Many thanks for your help

    http://www.designs2print.co.uk/image...ay%20page.html

  11. #11
    Join Date
    May 2013
    Posts
    8
    Its the bit where it says

    Order your leaflets today [TODAYS DATE] and receive your order on [4 DAYS LATER DATE]

  12. #12
    Join Date
    Sep 2007
    Location
    istanbul
    Posts
    317
    Code:
      
    <head>
    <style type="text/css">
    /* http://www.w3schools.com/css/tryit.asp?filename=trycss_border-style   */
    .renkli { font-size:20px; background-color:#ada; color: blue; border-style:dotted;padding:3px;}
    </style>
    </head>
    <body>
    Order yourleaflets today [ <span class="renkli" id="today"></span> ] and receive your order on [ <span class="renkli" id="fourDaysLater"></span> ]
    <script type="text/javascript">
    // http://www.webdeveloper.com/forum/showthread.php?277859-Help-with-Showing-Dates-in-Html-Pages
    
    // http://www.w3schools.com/js/js_obj_date.asp
    
    // http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_date_weekday
    function format ( num ){
    return ( num < 10) ? '0'+num  : num ; 
    }
    
    var d = new Date();
    
    var el= document.getElementById('today');
    el.innerHTML = format( d.getDate() ) + "/"+ format( d.getMonth() ) + "/"+ d.getFullYear();
    
    var i, n, count=0,  day= d.getDay();
    
    for(i=1; ; i++) {
    
     if ( ((1 + day + i )%7) > 1  ) {  count++;  }
    //  n = (day + i) %7 ;
    //  if( n != 0  && n != 6 )   {   count++;  }
       if(count == 4) { break;}
    }
    
    var tarih = new Date(d.setDate(d.getDate() + i));
    
    var parantez= document.getElementById('fourDaysLater');
    
    parantez.innerHTML = format( tarih.getDate() ) + "/"+ format( tarih.getMonth() ) +"/"+ tarih.getFullYear();
    
    </script>
    </body>

  13. #13
    Join Date
    Sep 2007
    Location
    istanbul
    Posts
    317
    format( d.getMonth() )
    must be
    format( d.getMonth() + 1)

    format( tarih.getMonth() )
    must be
    format( tarih.getMonth() + 1)



    var i, n=4, day= d.getDay();

    for(i=1; i<=4 ; i++) {

    if ( (( day + i )%7 ) == 6 ) { n= 6; break; }
    if ( (( day + i )%7 ) == 0 ) { n= 5; break; }

    }

    var tarih = new Date(d.setDate(d.getDate() + n));

    Code:
      
        <head>
        <style type="text/css">
        /* http://www.w3schools.com/css/tryit.asp?filename=trycss_border-style   */
        .renkli { font-size:20px; background-color:#ada; color: blue; border-style:dotted;padding:3px;}
        </style>
        </head>
        <body>
        Order yourleaflets today [ <span class="renkli" id="today"></span> ] and receive your order on [ <span class="renkli" id="fourDaysLater"></span> ]
        <script type="text/javascript">
        // http://www.webdeveloper.com/forum/showthread.php?277859-Help-with-Showing-Dates-in-Html-Pages
    
        // http://www.w3schools.com/js/js_obj_date.asp
    
        // http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_date_weekday
       
        function format ( num ){
        return ( num < 10) ? '0'+num  : num ; 
        }
    
        var d = new Date();
    
        var el= document.getElementById('today');
        el.innerHTML = format( d.getDate() ) + " / "+ format( d.getMonth() +1 ) + " / "+ d.getFullYear();
    
        var i, n=4,  day= d.getDay();
    
        for(i=1; i<=4 ; i++) {
    
         if ( (( day + i )%7 ) == 6  ) { n= 6; break;  }
         if ( (( day + i )%7 ) == 0  ) { n= 5; break;  }
        
        }
    
        var tarih = new Date(d.setDate(d.getDate() + n));
    
        var parantez= document.getElementById('fourDaysLater');
    
        parantez.innerHTML = format( tarih.getDate() ) + " / "+ format( tarih.getMonth() +1 ) +" / "+ tarih.getFullYear();
    
        </script>
        </body>
    Last edited by Ayşe; 05-14-2013 at 05:48 PM.

  14. #14
    Join Date
    May 2013
    Posts
    8
    Just wanted to say a BIG THANK YOU! it works perfectly

    PS if i wanted to change 4 days to 5 days what section to i edit?

  15. #15
    Join Date
    Sep 2007
    Location
    istanbul
    Posts
    317
    var i, n=5, day= d.getDay();

    for(i=1; i<=5 ; i++) {

    if ( (( day + i )%7 ) == 6 ) { n= 7; break; }
    if ( (( day + i )%7 ) == 0 ) { n= 6; break; }
    Last edited by Ayşe; 05-15-2013 at 12:51 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles