Page 1 of 1

Validar fecha.

Posted: Mon Jan 04, 2021 8:25 pm
by FiveWiDi
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.

Re: Validar fecha.

Posted: Tue Jan 05, 2021 7:35 pm
by James Bott
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: Select all

#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

Re: Validar fecha.

Posted: Wed Jan 06, 2021 9:14 pm
by nageswaragunupudi
if Empty( CTOD( cStr ) )
// invalid date
endif

Re: Validar fecha.

Posted: Wed Jan 06, 2021 10:43 pm
by FiveWiDi
Thank you very much mr. Bott and mr. Rao

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

Code: Select all

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.

Re: Validar fecha.

Posted: Thu Jan 07, 2021 9:43 am
by Enrico Maria Giordano
FiveWiDi wrote:

Code: Select all

FUNCTION lIsDateOk( dDate )
Return ( .Not. Empty( CToD( DToC( dDate ) ) ) )
Why not just this?

Code: Select all

IF EMPTY( dDate )
    // error
ENDIF
EMG

Re: Validar fecha.

Posted: Thu Jan 07, 2021 8:08 pm
by FiveWiDi
Enrico Maria Giordano wrote:
FiveWiDi wrote:

Code: Select all

FUNCTION lIsDateOk( dDate )
Return ( .Not. Empty( CToD( DToC( dDate ) ) ) )
Why not just this?

Code: Select all

IF EMPTY( dDate )
    // error
ENDIF
EMG
Because I have found a date with value 04/22/ 320

Re: Validar fecha.

Posted: Thu Jan 07, 2021 8:14 pm
by Enrico Maria Giordano
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

Re: Validar fecha.

Posted: Thu Jan 07, 2021 9:05 pm
by FiveWiDi
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"

Re: Validar fecha.

Posted: Fri Jan 08, 2021 8:58 am
by nageswaragunupudi
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 !!!

Re: Validar fecha.

Posted: Fri Jan 08, 2021 9:09 am
by Enrico Maria Giordano
Thank you for the confirmation, Master.

EMG