www.webdeveloper.com
Results 1 to 10 of 10

Thread: Manipulating Dates

  1. #1
    Join Date
    May 2006
    Posts
    92

    Manipulating Dates

    Hi ya,

    I'm having trouble working with the date object. I'm trying to add seven to the current date, but if I do say something like:

    Code:
    var forwardDate = currentDate+7;
    That gives me the wrong date, if the date is greater than say 31, as then it won't be even a day in the calender.

    Is there a way to make sure that if the date rolls past 31 it'll go down to the 1st or something?

    I hope I make sense....

  2. #2
    Join Date
    May 2006
    Posts
    92
    I've also tried another method of grabbing the entire date string and turning it into an array but that causes and error and I dont know why.

    Err I'm well confused

    Code:
    var myDate=new Date()
    myDate.setDate(myDate.getDate()+5)
    
    var testStr = myDate;
    testStr = testStr.split(' ');
    document.write("<br/>");
    var arrayIndex;
    
    for (arrayIndex in testStr)
    	{
    		document.write(testStr[arrayIndex] + "<br/>");
    	}

  3. #3
    Join Date
    Feb 2006
    Posts
    2,927
    When you get a date from a Date object you get an integer,
    it no longer 'knows' anything about the date object except
    for the number of the date.

    To add a day to a date and return a new date you need
    to increment the object and then read the new value:

    I work with dates enough to make adding (or subtracting)
    days and parts of days a method for all Dates, though you can use the same
    code in a function call:

    Code:
    Date.prototype.addpartDay= function(n){
    		if(!n) n= 1;
    		n*=(86400000);
    		var d= (this.getTime()+n);
    		return  new Date(d);
    	};
    Or to add whole days:
    Code:
    	Date.prototype.addDays= function(n,boo){
    		if(!n) n= 1;		
    		var d= this.setDate(this.getDate()+n)
    		if(boo) d= d.toUTCString();
    		return d;
    	};
    Last edited by mrhoo; 05-23-2006 at 11:35 AM.

  4. #4
    Join Date
    May 2006
    Posts
    92
    I've kinda worked out a work around solution but it doesn't work?

    Code:
    var myDate=new Date()
    myDate.setDate(myDate.getDate()+5)
    
    var testStr = myDate;
    testStr = testStr.split(' ');
    document.write("<br/>");
    var arrayIndex;
    
    for (arrayIndex in testStr)
    	{
    		document.write(testStr[arrayIndex] + "<br/>");
    	}

  5. #5
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,278
    Code:
    var myDate=new Date()
    myDate.setDate(myDate.getDate()+5)
    Is the correct way to add five days to the current date. If you try adding ticks you'll run afoul of Daylight Savings Time.

    You don't need to be playing around with the string version of the date unless you are up to something strange. What exactly are you up to?
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  6. #6
    Join Date
    May 2006
    Posts
    92
    i need to split the date object.... i thought that was self-evident, and then prepoluate a form.

  7. #7
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,278
    You want to use the various methods of the Date object to populate the form. Methods like Date.getMonth and Date.getFullYear.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  8. #8
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    What about?:
    PHP Code:
    <script type="text/javascript">
    var 
    plus=7;
    var 
    today=new Date();
    today.setTime(today.getTime()+(1000*60*60*24*plus))
    alert(today)
    </script> 

  9. #9
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,278
    As I mentioned above, in some parts of the world that will go awry twice a year.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  10. #10
    Join Date
    Jan 2006
    Location
    I'm in GMT -5
    Posts
    561
    gingerj:

    Please check your private messages.

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