How do I calculate quarters since a specific date

jpirozzolo
jpirozzolo used Ask the Experts™
on
Hi Experts,  I wonder if anyone can help with a Javascript query.  

I have a date that comes in from a database and I would like to determine the amount of quarters since that date using Javascript (1 quarter = three months) .
(However, the date comes in as text so it may need to be converted to a date format first).

eg:
var startDate = 'dd-mm-yy'

Could we have the result as an integer, so it only counts a quarter if the difference in the date is a full three months?

Thanks in advance for your help

Regards Joe.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Michel PlungjanIT Expert
Top Expert 2009

Commented:
1. what is the text format of the date?
2. a quarter is normally from jan-mar, apr-jun, jul-sep, oct-nov

if you want to say it is 3 quarters ago, I guess you can, but how precisely do you want to be?

If you find the next 1st of the month and then go back 3 months-1 you will get the start of the current quarter period, but not the start of the actual quarter as in Q3/2010
not sure what you meant <<a quarter since that date>>, but you can get the current month using getMonth() method of date
http://www.w3schools.com/jsref/jsref_getmonth.asp

and then you can add 3 to it, and set that new month value using setMonth(), you will get next three month ahead date.

Let me know what i am missing

Author

Commented:
Hi mplungjan

Thanks for your fast reply.

The text format I beleive is a string (although the database has it as date).  What happens is the date comes into the HTML document via a system I have no control over.  So I have

var startDate = {%StartDate%}    and the system converts it to var startDate = 'dd-mm-yy' when the HTML document is loaded.

So for this purpose, a quater is three months from the date declared.  So if the startdate is 01-04-10 (April) then a full quater will be clocked up when the current date is > 01-07-10.



OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

Author

Commented:
Hi gurvinder372
Basically what I mean is if the startdate is 01-04-10 (April) then a full quater will be clocked up when the current date is > 01-07-10.

So I am really after a number to be returned of the quaters clocked up from the startdate to the current (todays) date.... hope this helps :)
you can use this method to get date object out of your string
function getDate(dateStr)
{
var dateValues = dateStr.split("-");
var d = new Date(dateValues[2] , dateValues[1], dateValues[0]);
return d;
}
this code worked for me
<html>
<body>

<script type="text/javascript">
function getDate(dateStr)
{
var dateValues = dateStr.split("-");
var d = new Date(dateValues[2] , dateValues[1], dateValues[0]);
return d;
}
function getNextQuarterDate(d)
{
   d.setMonth(d.getMonth()+3)
   return d;
}
var dateStr = "01-07-10";

alert(getNextQuarterDate(getDate(dateStr)).toString());

</script>

</body>
</html>

Open in new window

Author

Commented:
Hi gurvinder372

Thanks for your code.  However, this adds a quater to the declated date.  I am after the amount of quaters from the declated date to todays date.  hope this is okay?  :)

Author

Commented:
Sorry, I'll type that again because my fingers are back to front!....

Hi gurvinder372

Thanks for your code.  However, this adds a quater to the declared date.  I am after the amount of quaters from the declared date to today's date.  hope this is okay?  :)
eg( quaters from "01-07-10" til now are 1)
check this one then
<html>
<body>

<script type="text/javascript">
function getDate(dateStr)
{
var dateValues = dateStr.split("-");
var d = new Date("20"+dateValues[2] , dateValues[1], dateValues[0]);
return d;
}
function getNextQuarterDate(d)
{
   d.setMonth(d.getMonth()+3)
   return d;
}

var currentdate = new Date();
var dateStr = "01-07-09";

var decDate = getDate(dateStr);

var numberOfQuaters = (currentdate.getFullYear() - decDate.getFullYear())*4;

var monthDiff = currentdate.getMonth() - decDate.getMonth();

numberOfQuaters+= monthDiff/4;

alert(numberOfQuaters);
</script>

</body>
</html>

Open in new window

Michel PlungjanIT Expert
Top Expert 2009

Commented:
guys it is quaRter

Author

Commented:
You'e uttely ight mplungjan.  I’m going to blame it on the lette ‘’ on my keyboad – unfotunately it’s faulty.  ;-D

Author

Commented:
Hi gurvinder372
That's it!  That's perfect and does exactly what I'm after.  Thanks very much for your fast reply :)

Thanks for everyone's input!!

Best Regards

Joe.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial