SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Feb 2008
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post Adding days to date

    Hi There,

    Could someone help me out please....
    For practically the whole day today i've been trying to figure out a way of adding the X number of days to todays date and displaying the resulting date in the body of my web page in the format Wednesday 14 May.

    I'm not a coder unfortunatley, so i have very limited knowledge of how to do it. I've mainly been using google to try and figure out but not as yet found anything that does exactly what i want or it is not explained properly how to implement the code.

    Could someone please help me out?

    The nearest code that i found is mentioned below:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <script type="text/javascript">
    function AddDays() {
    var now=new Date();
    var newdate=new Date();
    var newtimems=newdate.getTime()+(14*24*60*60*1000);
    newdate.setTime(newtimems);
    document.write(newdate.toLocaleString());
    }
    </script>
    </head>

    <body>
    <p>Signup Before
    <script type="text/javascript">
    <!--
    AddDays();
    //-->
    </script>
    </p>
    </body>
    </html>

    But it doesn't display it in the format i want. If anyone is able to help me i'd be most grateful

    Thanks
    Humayun

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    4 Thread(s)
    Code javascript:
    function addDays(days) {
        newdate = new Date();
        newdate.setDate(newdate.getDate()+days);
        return newdate.toDateString();
    }
    document.write(addDays(14));

    And you will get "Tue Jun 03 2008"

    If you want more control over the date format, there is a nice library at http://blog.stevenlevithan.com/archi...te-time-format that lets you specify precisely how you want the text formatted.
    Last edited by paul_wilkins; May 14, 2008 at 18:49.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Member
    Join Date
    Feb 2008
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your message. I can see this may be the answer to what i want to do. But i seem to be having troubles implementing this dateFormat code. It may very well be my complete lack of knowledge on Java programming and I am attempting this for the very first time. But i created a test page to try and see the results of the date format. I can't seem to get it run and display the DateFormat code. I've downloaded the js file from the link you supplied. Put it in the same directory on my web server as the web page and tried calling it but it's not worked. Here is the code:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <script type="text/javascript">
    function addDays(days) {
    newdate = new Date();
    newdate.setDate(newdate.getDate()+days);
    return newdate.toDateString();
    }

    </script>
    </head>

    <body>
    <p>I can't guarantee this after
    <script type="text/javascript">
    <!--
    document.write(addDays(14));
    //-->
    </script>
    </p>
    <p>Date Format 2:
    <script language="Javascript" src="date.format.js">
    <!--
    document.write(dateFormat((addDays(14)),"dddd d mmmm"))
    //-->
    </script>
    </p>
    </body>
    </html>

    Could you tell me what i've done wrong? Thank you in advance...

  4. #4
    doRighteousDeeds++
    Join Date
    Aug 2006
    Location
    تركيا Turkey Türkiye
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
      
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <script type="text/javascript">
    function addDays(days) {
    newdate = new Date();
    newdate.setDate(newdate.getDate()+days);
    return newdate.toDateString();
    }
    
    </script>
    </head>
    
    <body>
    <p>I can't guarantee this after
    <script type="text/javascript">
    <!--
    document.write(addDays(14));
    //-->
    </script>
    </p>
    <p>Date Format 2:
    <script type="text/javascript" src="date.format.js"></script>
    <script type="text/javascript">
    <!--
    document.write(dateFormat((addDays(14)),"dddd d mmmm"))
    //-->
    </script>
    </p>
    </body>
    </html>
    Code:
      
    <script type="text/javascript">
    Date.prototype.addDays=function(days){
    this.setDate(this.getDate()+days);
    return this;
    }
    
    Date.prototype.dddd=["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
    
    Date.prototype.mmmm=["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
    
    Date.prototype.dddd_d_mmmm=function(){
    return this.dddd[this.getDay()]+"  " +this.getDate()+"   "+this.mmmm[this.getMonth()];
    }
    
    Date.prototype.stringe=function(days){
    return this.toDateString();
    }
    var a = new Date();
    var b= a.addDays(14);
    alert(b);
    alert(b.stringe());
    alert(b.dddd_d_mmmm());
    
    </script>
    Last edited by muazzez; May 15, 2008 at 08:20.
    The Time Through Ages. In the Name of Allah, Most Gracious, Most Merciful.
    1. By the Time, 2. Verily Man is in loss,
    3. Except such as have Faith, and do righteous deeds, and (join together) in the mutual enjoining of Truth, and of Patience and Constancy.

  5. #5
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    function addDays(days) {
        var newDate = new Date();
        newDate.setDate(newDate.getDate() + days);
        return newDate.toDateString();
    }
    
    document.write(addDays(14));
    Picky maybe, but a global variable will be created if var is omitted.

  6. #6
    SitePoint Member
    Join Date
    Feb 2008
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's is fantastic. Thanks for that. Works just as i want it.

    Now i went to incorporate that code into my web page uploaded all the files to the website and it doesn't seem to work.
    When the web page is laoded there is an error 'Object Expected' starting on the line the code is implemented on.

    I put a section of the code in the head tag of the HTML and then the execution of the script in the body of the HTML but getting an error. You can see the actual code on the following link: http://www.Refundagent.co.uk/new/date.html ?

    The code is towards the bottom of the page as the date should display right at the end.

    Is there anything else i need to do?

  7. #7
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You incorrectly inserted several <script> tags within the <style> tag...

  8. #8
    SitePoint Member
    Join Date
    Feb 2008
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    superb...thank you all for your help....


Bookmarks

Posting Permissions

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