FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Number day of the week
Posts: 7317
Joined: Thu Oct 18, 2012 07:17 PM
Number day of the week
Posted: Sat Mar 07, 2015 08:50 PM

I tried with Dow(date()) but it give me 7 ( today is 07.03.2015) and I wish have 6
Any solution ?

Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)

I use : FiveWin for Harbour March-April 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
Posts: 159
Joined: Wed Mar 28, 2007 01:19 PM
Re: Number day of the week
Posted: Sat Mar 07, 2015 09:53 PM

strange... my old dbase III gives the same result...
what I can do?
Regards, Euclides

Posts: 4043
Joined: Wed Dec 19, 2007 06:40 PM
Re: Number day of the week
Posted: Sat Mar 07, 2015 10:06 PM
Returns 6



From resource

REDEFINE SAY oSay[1] PROMPT "Start : " ID 110 OF oDlg UPDATE COLOR 8421376 FONT oFont1
REDEFINE DTPICKER oGet[1] VAR Start_date ID 120 OF oDlg UPDATE
oGet[1]:SetFont( oFont1 )
REDEFINE SAY oSay[2] PROMPT "End : " ID 210 OF oDlg UPDATE COLOR 255 FONT oFont1
REDEFINE DTPICKER oGet[2] VAR End_date ID 220 OF oDlg UPDATE
oGet[2]:SetFont( oFont1 )
oGet[2]:bChange := {|| ( nDays := WKDAYS( start_date, end_date ), oSay[4]:Refresh() ) }

REDEFINE SAY oSay[3] PROMPT "Days : " ID 310 OF oDlg UPDATE COLOR 255, 255 FONT oFont1
REDEFINE SAY oSay[4] VAR nDays ID 320 OF oDlg UPDATE COLOR 255, 16776960 FONT oFont2


Code (fw): Select all Collapse
FUNCTION MAIN()
LOCAL start_date := CTOD("01.03.2015") 
LOCAL End_date := CTOD("07.03.2015")

SET DATE GERMAN
SET DATE FORMAT "DD.MM.YYYY"

MsgAlert( WKDAYS( start_date, end_date ), "Days" )

RETURN( NIL )

// -----------------------

FUNCTION WKDAYS( start_date, end_date )
MEMVAR tr_comp, tr_days, tr_weekdays
PRIVATE tr_comp, tr_days, tr_weekdays
tr_comp = 0

// Syntax: WKDAYS( <start date>, <end date> )
// Return: <expN> number of days between two dates
// Note..: Both parameters are <expD>

DO CASE
     CASE start_date > end_date
          MsgAlert( "Error -- start date is greater than end date", "ERROR" )
          RETURN -1
     CASE start_date == end_date .AND. .NOT. ISWKEND( end_date ) 
          RETURN  1
     CASE ( start_date == end_date .AND. ISWKEND( end_date ) ) .OR.  ;
          ( ISWKEND(start_date)  .AND. ISWKEND(end_date)   .AND. ;
          ( end_date - start_date ) == 1 ) 
          RETURN  0
ENDCASE

IF ( end_date - start_date < 6 )
     DO WHILE ( start_date <= end_date )
          IF .NOT. ISWKEND( start_date )
               tr_comp = tr_comp + 1
          ENDIF
          start_date = start_date + 1
     ENDDO
     RETURN(tr_comp)
ENDIF

DO WHILE ISWKEND( start_date )      // If start is on a weekend skip past it
     start_date = start_date + 1
ENDDO

DO WHILE DOW( start_date ) # 2      // If start is on any day but Monday
     start_date = start_date + 1            // compensate to monday w/o weekends
     IF .NOT. ISWKEND( start_date ) 
          tr_comp = tr_comp + 1
     ENDIF
ENDDO

DO WHILE ISWKEND( end_date )     // do the same for the end date
     end_date = end_date - 1
ENDDO

DO WHILE DOW( end_date ) # 2 
     end_date = end_date -1
     IF .NOT. ISWKEND( end_date )
          tr_comp = tr_comp + 1
     ENDIF
ENDDO
tr_days     = ( end_date - start_date ) + 1
tr_weekdays = tr_days - ( INT( tr_days  / 7 ) * 2 )

RETURN     ( tr_weekdays + tr_comp ) 

// ----------------------------------------

FUNCTION ISWKEND(tr_date)
// Syntax: ISWKEND( <expD> )
// Parameters  date tr_date 
// Return: True if <expD> occurs on a Saturday or Sunday, otherwise False

lOK := .F.

IF DOW(tr_date) = 7
     lOK := .T.
ENDIF

RETURN ( lOK )


best regards
Uwe :-)
Since 1995 ( the first release of FW 1.9 )

i work with FW.

If you have any questions about special functions, maybe i can help.
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Number day of the week
Posted: Sat Mar 07, 2015 11:37 PM
Either
Code (fw): Select all Collapse
#xtranslate MyDOW( <d> ) => DOW( <d> - 1 )


or

Code (fw): Select all Collapse
function MyDOW( d ); return DOW( d - 1 )
Regards



G. N. Rao.

Hyderabad, India
Posts: 4043
Joined: Wed Dec 19, 2007 06:40 PM
Re: Number day of the week
Posted: Sun Mar 08, 2015 12:35 AM
Mr. Rao,

I think it is not as easy.
It must be calculated.
As well Eastern, Christmas .... must be respected.
I will include this solution inside the calculation.
The original calculated value would be 18 days for this sample ( only Sunday is repected )
The start- and end-day is not included because of defined BETWEEN !!!
What's about SATURDAY ?



best regards
Uwe :-)
Since 1995 ( the first release of FW 1.9 )

i work with FW.

If you have any questions about special functions, maybe i can help.
Posts: 3358
Joined: Fri Oct 07, 2005 08:20 PM
Re: Number day of the week
Posted: Sun Mar 08, 2015 03:31 AM
Ukoeing:

Silvio is asking for the day of the week, not for the number of days between two dates.

From clipper doc:
DOW() returns the day of the week as a number between one and seven,
The first day of the week is one (Sunday) an the last day is seven (Saturday)
then the mr. rao's advice is a good option.

Or

Code (fw): Select all Collapse
nDay := DOW(Date()) -1


Regards
SOI, s.a. de c.v.
estbucarm@gmail.com
http://www.soisa.mex.tl/
http://sqlcmd.blogspot.com/
Tel. (722) 174 44 45
Carpe diem quam minimum credula postero
Posts: 137
Joined: Mon Oct 22, 2012 04:43 PM
Re: Number day of the week
Posted: Sun Mar 08, 2015 08:07 AM
Code (fw): Select all Collapse
nDay := DOW(Date()) -1
if nDay = 0;  nDay := 7; endif
Regards



Ing. Anton Lerchster
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Number day of the week
Posted: Sun Mar 08, 2015 08:35 AM
alerchster wrote:
Code (fw): Select all Collapse
nDay := DOW(Date()) -1
if nDay = 0;  nDay := 7; endif

DOW( dDate - 1 ) is a single step solution
Regards



G. N. Rao.

Hyderabad, India
Posts: 38
Joined: Tue Mar 04, 2008 03:44 PM
Re: Number day of the week
Posted: Sun Mar 08, 2015 12:09 PM
Hi Uwe,
Is to want this?

Code (fw): Select all Collapse
FUNCTION WKDAYS( start_date, end_date )
      LOCAL nDays := 0
      LOCAL dDate := start_date

      Do While dDate<=end_date
          IF DOW(dDate)=1 .OR. DOW(dDate)=7  // Sunday, Saturday
          ELSE
              nDays++
          ENDIF
          dDate++
      ENDDO
      RETURN nDays
Regards,

Moon

FWH 16.11 | xHarbour | Harbour | BCC72 | DBF | ADS | MySQL | DrLib
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Number day of the week
Posted: Sun Mar 08, 2015 12:11 PM
Mr Uwe

My answer was for what Mr Silvio and Mr Euclides asked.


Mr. Rao,

I think it is not as easy.
It must be calculated.


I agree if the purpose is the calculate the number of working days starting from a date and ending with another date. Please check if this function works.
Code (fw): Select all Collapse
function WorkingDays( d1, d2 )

   local x, w

   if ( d2 -= If( ( w := DOW( d2 - 1 ) ) > 5, w - 5, 0 ) ) >= ;
      ( d1 += If( ( w := DOW( d1 - 1 ) ) > 5, 8 - w, 0 ) )

      x  := d2 - d1
      x  -= ( ( ( Int( x / 7 ) + If( DOW( d2 ) >= DOW( d1 ), 0, 1 ) ) * 2 ) - 1 )
   else
      x  := 0
   endif

return x


This function applies to 5-day week where Saturday and Sunday are observed holidays.
Regards



G. N. Rao.

Hyderabad, India
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Number day of the week
Posted: Sun Mar 08, 2015 12:57 PM
Extended the function to handle 5 or 6 day week and a list of holidays
Code (fw): Select all Collapse
function WorkingDays( d1, d2, nWkgDays, aHolidays )

   local x, w

   DEFAULT nWkgDays   := 5, aHoliDays := {}

   nWkgDays     := Min( 6, Max( 5, nWkgDays ) )

   if ( d2 -= If( ( w := DOW( d2 - 1 ) ) > nWkgDays, w - nWkgDays, 0 ) ) >= ;
      ( d1 += If( ( w := DOW( d1 - 1 ) ) > nWkgDays, 8 - w, 0 ) )

      x  := d2 - d1
      x  -= ( ( Int( x / 7 ) + If( DOW( d2 ) >= DOW( d1 ), 0, 1 ) ) * ( 7 - nWkgDays ) - 1 )
      AEval( aHolidays, { |d| If( d >= d1 .and. d <= d2 .and. DOW( d ) < 7 - nWkgDays, x--, nil ) } )
   else
      x  := 0
   endif

return x


The solution provided by Mr dbmanfwh is elegant, simple, easy to understand, easy to maintain and highly flexible. Being an old timer, I have a tendency to avoid loops in oft-used functions, but given present-day's hardware loops are no more expensive. His solution is worth implementing. This has the merit of adopting to any week-day holidays. For example, some countries have Friday as weekly holiday. It is very easy to adopt his function.
Regards



G. N. Rao.

Hyderabad, India
Posts: 7317
Joined: Thu Oct 18, 2012 07:17 PM
Re: Number day of the week
Posted: Tue Mar 10, 2015 08:02 AM

Mr Rao
in a Holyday I must insert some dates ?

Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)

I use : FiveWin for Harbour March-April 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
Posts: 7317
Joined: Thu Oct 18, 2012 07:17 PM
Re: Number day of the week
Posted: Tue Mar 10, 2015 08:12 AM

Mr Rao , this function is right to calc the number day of the week ?

Function nWeekDay(dDate)

  local nWeekDay

    nWeekDay    := DoW(  dDate-1 )

return nWeekday
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)

I use : FiveWin for Harbour March-April 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
Posts: 1283
Joined: Fri Feb 10, 2006 02:34 PM
Re: Number day of the week
Posted: Tue Mar 10, 2015 08:54 AM

Rao,

I like it , tanks a lot!

Salutacions, saludos, regards

"...programar es fácil, hacer programas es difícil..."

UT Page -> https://carles9000.github.io/
Forum UT -> https://discord.gg/bq8a9yGMWh
HIX -> https://github.com/carles9000/hix
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: Number day of the week
Posted: Wed Mar 11, 2015 08:30 PM

Silvio,

Since you are looking for the day of work-week (starting with Monday through Saturday), how about:

// Day of work-week, starting on Monday
function DOWW( dDate )
Return DOW( dDate - 1 )

FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10

Continue the discussion