javascript get start and end dates of period

Hi,
 I'm trying to write a quick javascript function (with very little success) to get the start date and the end date(plus 1) of the period. So for example today is march 23rd so the start date is feb 25th, and the end date is march 31 so the returns for the function would be feburary 25th and April 1st.
Can anyone help please?
toby_langAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TrakosCommented:
I can suggest you using javascript date object. It is very handy in that sort of functions.
There were some similiar problem recently, just like
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_20391606.html?sfQueryTermInfo=1+date+javascript .
Also you could check some other sites, e.g. http://www.comptechdoc.org/independent/web/cgi/javamanual/javadate.html (this one is a bit old, but still rather good)
0
ZylochCommented:
Another question I have is how to determine the start date. (maybe I'm just ignorant?)
0
toby_langAuthor Commented:
Hi thanks- I really should have clarified the question a bit better.

Period End = the last Saturday of every month.
Period Start = the day after a period end.
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

toby_langAuthor Commented:
I made an example in vb script, a language I am a little better at :) If someone could help get this into javascript and turn it into a function instead that would be great.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/vbscript">
W = DateSerial(Year(Now), Month(Now), 1) ''find the start of this month
D = W - 1 - (WeekDay(W)-1) mod 7
document.write "start of current period" & D &"<br>"

W = DateSerial(Year(Now), Month(Now)+1, 1) ''find the start of this month
D = W - 1 - (WeekDay(W)-1) mod 7
document.write "end of current period " & D&"<br>"
</script>
<title></title>
</head>
<body>
</body>
</html>
0
TrakosCommented:
What about e.g. 26th Feb 2007, should it return as a start 24th Feb or 27th Jan?
I mean, what about when it is AFTER the last Saturday but still the same month, in your function it starts from the 1st of current month, so it will return 27th Jan, which isn't correct, right?
And one more doubt: you said last Saturday but you give as an example "march 23rd so the start date is feb 25th", but 25th was actually Sunday?
0
toby_langAuthor Commented:
You are right, Feb 28th should return feb 24th as the start and march 31st end. And you are right again I mean sunday :(
0
toby_langAuthor Commented:
Here is an updated VB script, can anyone help me with the javascript equivalent?

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/vbscript">
WDate = DateSerial(Year(Now),2,25)

//end date
W = DateSerial(Year(WDate), Month(WDate)+1, 1) ''find the start of this month
D = W - 1 - (WeekDay(W)-1) mod 7

If(D < WDate) Then
    W = DateSerial(Year(WDate), Month(WDate)+2, 1) ''find the start of this month
    D = W - 1 - (WeekDay(W)-1) mod 7
    document.write "end of current period " & D &"<br>"
    W = DateSerial(Year(WDate), Month(WDate), 1) ''find the start of this month
    D = W - 1 - (WeekDay(W)-1) mod 7
    document.write "start of current period" & D &"<br>"
Else
    document.write "end of current period " & D &"<br>"
    W = DateSerial(Year(WDate), Month(WDate), 1) ''find the start of this month
    D = W - 1 - (WeekDay(W)-1) mod 7
    document.write "start of current period" & D &"<br>"
end If
</script>
<title></title>
</head>
<body>
</body>
</html>
0
toby_langAuthor Commented:
I can't get the javascript code going- can anyone help?

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/vbscript">
WDate = Now
//calc the end date
W = DateSerial(Year(WDate), Month(WDate)+1, 21)
document.writeln W & "<br>"
D = W - 1 - (WeekDay(W)-1) mod 7
document.writeln D &"<br>"
 
//if the end date is less than today
If(D < WDate) Then
    W = DateSerial(Year(WDate), Month(WDate)+2, 1) ''find the start of this month
    D = W - 1 - (WeekDay(W)-1) mod 7
   '' document.writeln "end of current period " & D &"<br>"
    W = DateSerial(Year(WDate), Month(WDate), 1) ''find the start of this month
    D = W - 1 - (WeekDay(W)-1) mod 7
    ''document.writeln "start of current period" & D &"<br>"
Else
    ''document.writeln "end of current period " & D &"<br>"
    W = DateSerial(Year(WDate), Month(WDate), 1) ''find the start of this month
    D = W - 1 - (WeekDay(W)-1) mod 7
    ''document.writeln "start of current period" & D &"<br>"
end If
</script>

<script type="text/javascript">
var WDate = new Date();
var W = new Date(WDate.getYear(),WDate.getMonth()+1,21);
document.writeln(W+ "<br>");
var D = new Date(W - 1 - W.getDay() % 7);
document.writeln(D);
</script>

<title></title>
</head>
<body>
</body>
</html>
0
toby_langAuthor Commented:
Got my own solution!


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
//Given a working date find the closest period end date that isn't less than today
function GetPeriodEndDate(WDate)
{
    var W = new Date(WDate.getYear(),WDate.getMonth()+1,1);         //create a date object defaulted to the first of next month
    var t = (1 - W.getDay()%7);                                     //find the offset to saturday
    PeriodEnd = new Date(W.getYear(),W.getMonth(),W.getDate()-2+t,WDate.getHours(),WDate.getMinutes(),WDate.getSeconds(),WDate.getMilliseconds());         //move the period back to the end of the period
   
    if(WDate > PeriodEnd)                                           //if the end of the period occured before today we need to move the period
    {
        W = new Date(WDate.getYear(),WDate.getMonth()+2,1);         //move the start date forward two months
        t = (1 - W.getDay()%7);
        PeriodEnd = new Date(W.getYear(),W.getMonth(),W.getDate()-2+t);
    }
   
    return PeriodEnd;
}

//Given a working date find the closest period start date that isn't less than today
function GetPeriodStartDate(WDate)
{
    var W = new Date(WDate.getYear(),WDate.getMonth()+1,1);         //create a date object defaulted to the first of next month
    var t = (1-W.getDay()%7);                                       //find the offset to saturday
    PeriodStart = new Date(W.getYear(),W.getMonth(),W.getDate()-2+t,WDate.getHours(),WDate.getMinutes(),WDate.getSeconds(),WDate.getMilliseconds());         //move the period back to the end of the period
   
    if(WDate > PeriodStart)                                         //if the end of the period occured before today we need to move the period    
        W = new Date(WDate.getYear(),WDate.getMonth()+1,1);         //move the start date forward one month
    else                                                            //no problems with the start date
        W = new Date(WDate.getYear(),WDate.getMonth(),1);           //start date was last month
   
    t = (1-W.getDay()%7);
    PeriodStart = new Date(W.getYear(),W.getMonth(),W.getDate()-1+t);
 
    return PeriodStart;
}
//today


var PS = GetPeriodStartDate(new Date());
document.writeln("Start Date: " + PS + "<br>");
var PE = GetPeriodEndDate(new Date());
document.writeln("End Date: " + PE + "<br>");

</script>
<title></title>
</head>
<body>
</body>
</html>
0
Computer101Commented:
PAQed with points refunded (125)

Computer101
EE Admin
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.