Validar fecha.

Validar fecha.

Postby FiveWiDi » Mon Jan 04, 2021 8: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 24.02 + Harbour 3.2.0dev (r2403071241), BCC 7.7 Windows 10
FiveWiDi
 
Posts: 1166
Joined: Mon Oct 10, 2005 2:38 pm

Re: Validar fecha.

Postby James Bott » Tue Jan 05, 2021 7: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: Select all  Expand view
#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
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Validar fecha.

Postby nageswaragunupudi » Wed Jan 06, 2021 9:14 pm

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

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10620
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Validar fecha.

Postby FiveWiDi » 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: Select all  Expand view
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 24.02 + Harbour 3.2.0dev (r2403071241), BCC 7.7 Windows 10
FiveWiDi
 
Posts: 1166
Joined: Mon Oct 10, 2005 2:38 pm

Re: Validar fecha.

Postby Enrico Maria Giordano » Thu Jan 07, 2021 9:43 am

FiveWiDi wrote:
Code: Select all  Expand view
FUNCTION lIsDateOk( dDate )
Return ( .Not. Empty( CToD( DToC( dDate ) ) ) )


Why not just this?

Code: Select all  Expand view
IF EMPTY( dDate )
    // error
ENDIF


EMG
User avatar
Enrico Maria Giordano
 
Posts: 8709
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Validar fecha.

Postby FiveWiDi » Thu Jan 07, 2021 8:08 pm

Enrico Maria Giordano wrote:
FiveWiDi wrote:
Code: Select all  Expand view
FUNCTION lIsDateOk( dDate )
Return ( .Not. Empty( CToD( DToC( dDate ) ) ) )


Why not just this?

Code: Select all  Expand view
IF EMPTY( dDate )
    // error
ENDIF


EMG


Because I have found a date with value 04/22/ 320
Un Saludo
Carlos G.

FiveWin 24.02 + Harbour 3.2.0dev (r2403071241), BCC 7.7 Windows 10
FiveWiDi
 
Posts: 1166
Joined: Mon Oct 10, 2005 2:38 pm

Re: Validar fecha.

Postby Enrico Maria Giordano » Thu Jan 07, 2021 8: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
User avatar
Enrico Maria Giordano
 
Posts: 8709
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Validar fecha.

Postby FiveWiDi » Thu Jan 07, 2021 9: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 24.02 + Harbour 3.2.0dev (r2403071241), BCC 7.7 Windows 10
FiveWiDi
 
Posts: 1166
Joined: Mon Oct 10, 2005 2:38 pm

Re: Validar fecha.

Postby nageswaragunupudi » Fri Jan 08, 2021 8: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
User avatar
nageswaragunupudi
 
Posts: 10620
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Validar fecha.

Postby Enrico Maria Giordano » Fri Jan 08, 2021 9:09 am

Thank you for the confirmation, Master.

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8709
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 30 guests