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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Day, Month, Date and Year

    Hi Guy's, Newbie here.

    I'm wondering if someone can help me. I'm trying to output the Day, Month, Date and Year but I'm getting into a spot of bother. I think I'm close, but just need to nail this one last part. My script is this.

    Code:
    function addDaysToDate(days) {
      var day = new Array("Mon","Tue","Wed","Thur","Fri","Sat","Sun");	
      var mths = new Array("Jan", "Feb", "Mar",
        "Apr", "May", "Jun", "Jul", "Aug", "Sep",
        "Oct", "Nov", "Dec");
    
      var d = new Date();
      d.setHours(d.getHours() + (24 * days));
      
      var currDa = d.getDay();
      var currD = d.getDate();
      var currM = d.getMonth();
      var currY = d.getFullYear();
    
      return day  [currDa] + " ," + currM + " " + currD + ", " + currY;
    }
    It was working fine with the months but I wanted to add the days and it's all gone to pot, can someone help me please. Thanks.

    Jonny

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    Days in Javascript run from Sunday (0) to Saturday (6).
    Code:
    <script type="text/javascript">
    
    function addDaysToToday( numDays ) {
        var date = new Date();
        date.setDate( date.getDate() + numDays );
        var months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
        var day = ["Sun", "Mon","Tue","Wed","Thur","Fri","Sat"];
        var dy = date.getDay();
        return day[dy] + " " + months[date.getMonth()] + " " + date.getDate() + ", " + date.getFullYear();
    }
    
    alert (addDaysToToday(7));
    
    // To subtract days from today, just pass a negative number to the function. 
    
    </script>

    One of the opposums was St.Matthew who was also a taximan.
    - Pupil's answer to Catholic Elementary School test.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    jonnystudent (01-31-2013)

  • #3
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Aha, thank you. Works a treat!


  •  

    Posting Permissions

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