Обсуждение: Weekends between
Hi guys,
I'm trying to find out how many weekends there's between 2 dates and Googling I found this algorithm from
http://archives.postgresql.org/pgsql-sql/2002-06/msg00304.php
I'm trying to find out how many weekends there's between 2 dates and Googling I found this algorithm from
http://archives.postgresql.org/pgsql-sql/2002-06/msg00304.php
date := now - day_of_the_week
> interval := interval + day_of_the_week
> date := date + int( interval/5)x7 + ( interval mod 5)
Assuming that now is current datetime, what I should expect from a date - int? or some1 could explain it better please?
> I'm trying to find out how many weekends there's between 2 dates and > Googling I found this algorithm from > http://archives.postgresql.org/pgsql-sql/2002-06/msg00304.php > > date := now - day_of_the_week >> interval := interval + day_of_the_week >> date := date + int( interval/5)x7 + ( interval mod 5) I'm no math wizz, but: I'd guess you would need to know the amount of days between start and end date How many full weeks there are in there And then add the weekend days that are located at the ends So end date minus (total days mod 7) with that information you can add (if needed another weekend or day) If anyone has something better, please, do correct me! HTH Regards, Serge Fonville
On 2009-07-30, Luiz Eduardo Cantanhede Neri <lecneri@gmail.com> wrote: > --00032557679e5694cf046fed47b1 > Content-Type: text/plain; charset=ISO-8859-1 > Content-Transfer-Encoding: 7bit > > Hi guys, > > I'm trying to find out how many weekends there's between 2 dates and > Googling I found this algorithm from > http://archives.postgresql.org/pgsql-sql/2002-06/msg00304.php "Skip Weekends" > date := now - day_of_the_week >> interval := interval + day_of_the_week >> date := date + int( interval/5)x7 + ( interval mod 5) > Assuming that now is current datetime, what I should expect from a > date - int? or some1 could explain it better please? int days ago. adding an int to a date adds that many days. assuming both input dates d1,d2 are weekdays weekends = floor((d2-'2007-08-02'::date)/7) - floor((d1-'2007-08-02')/7) (the above counts sat-sun transitions)