Validar fecha.

Post Reply
FiveWiDi
Posts: 910
Joined: Mon Oct 10, 2005 2:38 pm

Validar fecha.

Post 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.
Un Saludo
Carlos G.

FiveWin 19.06 + Harbour 3.2, BCC 7 Windows 10
User avatar
James Bott
Posts: 4654
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA
Contact:

Re: Validar fecha.

Post 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
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: Validar fecha.

Post by nageswaragunupudi »

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

G. N. Rao.
Hyderabad, India
FiveWiDi
Posts: 910
Joined: Mon Oct 10, 2005 2:38 pm

Re: Validar fecha.

Post 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.
Un Saludo
Carlos G.

FiveWin 19.06 + Harbour 3.2, BCC 7 Windows 10
User avatar
Enrico Maria Giordano
Posts: 7355
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Contact:

Re: Validar fecha.

Post 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
FiveWiDi
Posts: 910
Joined: Mon Oct 10, 2005 2:38 pm

Re: Validar fecha.

Post 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
Un Saludo
Carlos G.

FiveWin 19.06 + Harbour 3.2, BCC 7 Windows 10
User avatar
Enrico Maria Giordano
Posts: 7355
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Contact:

Re: Validar fecha.

Post 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
FiveWiDi
Posts: 910
Joined: Mon Oct 10, 2005 2:38 pm

Re: Validar fecha.

Post 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"
Un Saludo
Carlos G.

FiveWin 19.06 + Harbour 3.2, BCC 7 Windows 10
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: Validar fecha.

Post 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 !!!
Regards

G. N. Rao.
Hyderabad, India
Post Reply