Need expert helpâ€”fast? Use the Help Bell for personalized assistance getting answers to your important questions.

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?

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?

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)

Period End = the last Saturday of every month.

Period Start = the day after a period end.

<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>

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?

<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>

<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

document.writeln(W+ "<br>");

var D = new Date(W - 1 - W.getDay() % 7);

document.writeln(D);

</script>

<title></title>

</head>

<body>

</body>

</html>

<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

var t = (1 - W.getDay()%7); //find the offset to saturday

PeriodEnd = new Date(W.getYear(),W.getMont

if(WDate > PeriodEnd) //if the end of the period occured before today we need to move the period

{

W = new Date(WDate.getYear(),WDate

t = (1 - W.getDay()%7);

PeriodEnd = new Date(W.getYear(),W.getMont

}

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

var t = (1-W.getDay()%7); //find the offset to saturday

PeriodStart = new Date(W.getYear(),W.getMont

if(WDate > PeriodStart) //if the end of the period occured before today we need to move the period

W = new Date(WDate.getYear(),WDate

else //no problems with the start date

W = new Date(WDate.getYear(),WDate

t = (1-W.getDay()%7);

PeriodStart = new Date(W.getYear(),W.getMont

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>

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

Computer101

EE Admin