FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Validar fecha.
Posts: 1446
Joined: Mon Oct 10, 2005 02:38 PM
Validar fecha.
Posted: Mon Jan 04, 2021 08:25 PM

Hola,

Estoy buscando una función que me permita validar una fecha; me refiero a que el fecha introducida sea correcta.

Así si el valor encontrado es 35/08/2018 pues que me indique que no es una fecha correcta.

Recuerdo de hace años haberla visto pero ahora no la localizo.

Gracias.

Un Saludo

Carlos G.



FiveWin 25.12 + Harbour 3.2.0dev (r2502110321), BCC 7.7 Windows 11 Home

Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: Validar fecha.
Posted: Tue Jan 05, 2021 07:35 PM
Hmm, I don't think you can enter an invalid date into a date variable type. Have to tried that?

Otherwise here is a test. Note that when a character date is converted to a date format date, if it is invalid it will be an empty date (" / / "). It is still a date data type but it is empty.

Code (fw): Select all Collapse
#include "fivewin.ch"

Function Main()
   Local dDate, cDate
   Set date BRITISH      // DD/MM/YYYY
   set epoch to 1980
   Set century on
   
   dDate:= ctod("35/08/2018")
   cDate:= dtoc(dDate)
   
   msgInfo(dDate,"dDate")
   
   msgInfo( ctod(cDate), "ctod(cDate)" )
   
   if dtoc(dDate) = "  /  /    "
      msgInfo("Valid date ", DateValid(dDate) )
   endif
   
Return nil

// Test for valid date
Function DateValid( dDate )
   Local lValid:=.t.
   if dtoc(dDate) = "  /  /    "
      lValid := .f.
   endif   
Return lValid 

// EOF
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Validar fecha.
Posted: Wed Jan 06, 2021 09:14 PM

if Empty( CTOD( cStr ) )
// invalid date
endif

Regards



G. N. Rao.

Hyderabad, India
Posts: 1446
Joined: Mon Oct 10, 2005 02:38 PM
Re: Validar fecha.
Posted: Wed Jan 06, 2021 10:43 PM
Thank you very much mr. Bott and mr. Rao

As Mr. Rao says, then this must be correct right?

Code (fw): Select all Collapse
FUNCTION lIsDateOk( dDate )
Return ( .Not. Empty( CToD( DToC( dDate ) ) ) )


The point is that I have a date field that must be corrupted and I want to check its validity.

Excuse my English, I'm using Google translate.

Un Saludo

Carlos G.



FiveWin 25.12 + Harbour 3.2.0dev (r2502110321), BCC 7.7 Windows 11 Home

Posts: 9022
Joined: Thu Oct 06, 2005 08:17 PM
Re: Validar fecha.
Posted: Thu Jan 07, 2021 09:43 AM
FiveWiDi wrote:
Code (fw): Select all Collapse
FUNCTION lIsDateOk( dDate )
Return ( .Not. Empty( CToD( DToC( dDate ) ) ) )


Why not just this?

Code (fw): Select all Collapse
IF EMPTY( dDate )
    // error
ENDIF


EMG
Posts: 1446
Joined: Mon Oct 10, 2005 02:38 PM
Re: Validar fecha.
Posted: Thu Jan 07, 2021 08:08 PM
Enrico Maria Giordano wrote:
FiveWiDi wrote:
Code (fw): Select all Collapse
FUNCTION lIsDateOk( dDate )
Return ( .Not. Empty( CToD( DToC( dDate ) ) ) )


Why not just this?

Code (fw): Select all Collapse
IF EMPTY( dDate )
    // error
ENDIF


EMG


Because I have found a date with value 04/22/ 320

Un Saludo

Carlos G.



FiveWin 25.12 + Harbour 3.2.0dev (r2502110321), BCC 7.7 Windows 11 Home

Posts: 9022
Joined: Thu Oct 06, 2005 08:17 PM
Re: Validar fecha.
Posted: Thu Jan 07, 2021 08:14 PM

Impossible. A date field can't contain an invalid date and even if the database were corrupted you wouldn't get the invalid date, you will get an empty date.

EMG

Posts: 1446
Joined: Mon Oct 10, 2005 02:38 PM
Re: Validar fecha.
Posted: Thu Jan 07, 2021 09:05 PM
Enrico Maria Giordano wrote:Impossible. A date field can't contain an invalid date and even if the database were corrupted you wouldn't get the invalid date, you will get an empty date.

EMG


Well, maybe.

Str( Year( oTdbfDomici:DATAFIRM ), 4 ) + "-" + Right( Str( 100 + Month( oTdbfDomici:DATAFIRM ), 3, 0), 2 ) + "-" + Right( Str( 100 + Day( oTdbfDomici:DATAFIRM ), 3, 0), 2 ) = " 320-04-22"

Un Saludo

Carlos G.



FiveWin 25.12 + Harbour 3.2.0dev (r2502110321), BCC 7.7 Windows 11 Home

Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Validar fecha.
Posted: Fri Jan 08, 2021 08:58 AM
Enrico Maria Giordano wrote:Impossible. A date field can't contain an invalid date and even if the database were corrupted you wouldn't get the invalid date, you will get an empty date.

EMG


ABSOLUTELY CORRECT !!!
Regards



G. N. Rao.

Hyderabad, India
Posts: 9022
Joined: Thu Oct 06, 2005 08:17 PM
Re: Validar fecha.
Posted: Fri Jan 08, 2021 09:09 AM

Thank you for the confirmation, Master.

EMG

Continue the discussion