Solved

javascript get start and end dates of period

Posted on 2007-03-23
11
519 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
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
 

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now