PDA

View Full Version : Dates and specifying XX days from now



Robert T.
Jul 19th, 2005, 09:44 PM
I need to be able to specify a date as 7 days from now, even if the day is next month. Is there a simple way to do this? I've been working with the following, but it seems like there must be simpler way to do what I'm looking for.

new Date(Date.UTC(defaultEndDate.getUTCFullYear(), defaultEndDate.getUTCMonth()+1,1)

Thanks,

Robert

rlemon
Jul 20th, 2005, 12:42 AM
i wrote a script like that for someone on the forums. let me find it and i'l get back to you.

rlemon
Jul 20th, 2005, 12:52 AM
this uses months but it shouldn't be hard to adapt it to days of the week.

http://codingforums.com/showthread.php?t=61345

A1ien51
Jul 20th, 2005, 01:20 AM
var dateFuture = new Date()
dateFuture.setDate(dateFuture.getDate()+7);

Eric

A1ien51
Jul 20th, 2005, 01:21 AM
whoops wrong thread....

Basscyst
Jul 20th, 2005, 01:37 AM
Is this what you need?



<script type="text/javascript">
function weekFromNow()
{
var now=new Date();
var dy=now.getDate()+7;
var mo=now.getMonth()+1;
var yr=now.getFullYear();
var later=mo+"/"+dy+"/"+yr;
later=new Date(later);
mo=later.getMonth()+1;
dy=later.getDate();
yr=later.getFullYear();
return mo+"/"+dy+"/"+yr;
}
alert(weekFromNow());
</script>


Basscyst

Willy Duitt
Jul 20th, 2005, 02:13 AM
You're not truly clear on what it is you want to do...
Do you want to know the date 7 days from the current date or that a date falls within 7 days from the current date...

Anyways, as rlemon mentioned, this forum is full of date scripts any number of which should be adaptable to fit your needs... Here is one I previously posted...



<script type="text/javascript">
<!--// Written By: [email protected] \\;
Number.prototype.addPrefix = function(){
if(this < 10){
return '0'+this;
} return this;
}

Date.prototype.getFutureDate = function(days){
var date = new Date(this.getTime());
date.setDate(date.getDate()+days);
date = (date.getMonth()+1).addPrefix()+'/'+date.getDate().addPrefix()+'/'+date.getFullYear();
return date;
}

// EXAMPLE USAGE \\;
alert(new Date().getFutureDate(7))

//-->
</script>


.....Willy

glenngv
Jul 20th, 2005, 05:34 AM
This is the most robust function. You can add or subtract any number to a date from milliseconds to year.


Date.prototype.add = function(number, interval){
var d = new Date(this);
if (typeof interval == "undefined" || interval==null) interval = "d"; //day (default)
switch(interval.toLowerCase()){
case "ms": //msec
d.setMilliseconds(d.getMilliseconds()+number);
break;
case "s": //sec
d.setSeconds(d.getSeconds()+number);
break;
case "n": //min
d.setMinutes(d.getMinutes()+number);
break;
case "h": //hr
d.setHours(d.getHours()+number);
break;
case "d": //day
default:
d.setDate(d.getDate()+number);
break;
case "m": //month
d.setMonth(d.getMonth()+number);
break;
case "y": //year
d.setFullYear(d.getFullYear()+number);
break;
}
return d;
}

//sample usage
var today = new Date();
var yesterday = today.add(-1, "d");
var tomorrow = today.add(1, "d");
var nextweek = today.add(7, "d");
var nextmonth = today.add(1, "m");
alert("today: " + today + "\nyesterday: " + yesterday + "\ntomorrow: " + tomorrow + "\nnextweek: " + nextweek + "\nnext month: " + nextmonth);