Link to home
Start Free TrialLog in
Avatar of toby_lang
toby_lang

asked on

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?
Avatar of Trakos
Trakos

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
https://www.experts-exchange.com/questions/20391606/JavaScript-Dates.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)
Avatar of Zyloch
Another question I have is how to determine the start date. (maybe I'm just ignorant?)
Avatar of toby_lang

ASKER

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.
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>
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?
You are right, Feb 28th should return feb 24th as the start and march 31st end. And you are right again I mean sunday :(
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>
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>
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>
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial