Solved

javascript get start and end dates of period

Posted on 2007-03-23
11
529 Views
Last Modified: 2012-05-05
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?
0
Comment
Question by:toby_lang
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 5

Expert Comment

by:Trakos
ID: 18783964
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
 
LVL 36

Expert Comment

by:Zyloch
ID: 18785976
Another question I have is how to determine the start date. (maybe I'm just ignorant?)
0
 

Author Comment

by:toby_lang
ID: 18787242
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:toby_lang
ID: 18787466
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
 
LVL 5

Expert Comment

by:Trakos
ID: 18788077
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
 

Author Comment

by:toby_lang
ID: 18788143
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
 

Author Comment

by:toby_lang
ID: 18788252
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
 

Author Comment

by:toby_lang
ID: 18789738
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
 

Author Comment

by:toby_lang
ID: 18790840
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
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 19544717
PAQed with points refunded (125)

Computer101
EE Admin
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question