Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How can I validate if a value is a valid date?
Message
De
11/01/2004 10:22:29
 
 
À
11/01/2004 09:54:23
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
00865825
Message ID:
00865828
Vues:
16
>I have searched for something like isdate() or similar function, but can't find any.
>So, how can I find if xValue is a valid date?
>I'm trying to write a function that translates any valid date to an easily read date value in Spanish, like 3/Ene/2004
>So far I managed to make the default value of a field = to convertdate(date()), but what if the user wants to write a different date and it happens that is not a valid date? Also I want the function to check for this, instead of having to write code for every date field in my application. Yes, it is in a very advanced state, and I thought of this just now.

Ulises,

Here's a function I use to set the ToolTipText of a DATE or DATETIME. It may help to code yours

For additional info on GetDateFormat see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/intl/nls_5w6s.asp
?date_Format(date())
*--------------------------------------------------------------------------
function Date_Format(d)

	#define	DATE_SHORTDATE	0x00000001
	#define	DATE_LONGDATE	0x00000002

	#define	LOCALE_SYSTEM_DEFAULT	0x00000800
	#define	LOCALE_USER_DEFAULT		0x00000400	
	* LCID GetThreadLocale(void)
	
	declare long GetThreadLocale in win32api
	
	*int GetDateFormat(
	*  LCID Locale,               // locale
	*  DWORD dwFlags,             // options
	*  CONST SYSTEMTIME *lpDate,  // date
	*  LPCTSTR lpFormat,          // date format
	*  LPTSTR lpDateStr,          // formatted string buffer
	*  int cchDate                // size of buffer
	*);
	
	declare integer GetDateFormat in win32api ;
			long	Locale, ;
			long	dwFlags, ;
			string@	lpDate, ;
			String@	lpFormat, ;
			string@	lpDateStr, ;
			integer	cchDate
	
	local lpDate, lpFormat, DateFormatted, nchars
	lpDate = SystemTimeStruct(d)
	DateFormatted = space(128)
	
	do case
	case !inlist(vartype(d), T_DATE, T_DATETIME )
		assert FALSE
		return ''
	endcase
	
	lpFormat = [ddd', 'dd MMM yyyy]

	nchars = GetDateFormat( GetThreadLocale(), 0, @lpDate, @lpFormat, @DateFormatted, len(DateFormatted))
	
	do case
	case empty(nchars)
		return ''
		
	case inlist(vartype(d), T_DATE)
		return left(DateFormatted, nchars-1)
		
	case inlist(vartype(d), T_DATETIME )
		return left(DateFormatted, nchars-1) + ' ' + ttoc(d,2)
	endcase
	
endfunc
*---------------------------------------------------------------------------
function SystemTimeStruct(d)
	local s
	
	do case
	case inlist(vartype(d), T_DATE, T_DATETIME )
		s =		BinToWord(year(d)) ;
			+	BinToWord(Month(d)) ;
			+	BinToWord(mod(dow(d,2),7)) ;
			+	BinToWord(day(d)) ;
			+	BinToWord(hour(d)) ;
			+	BinToWord(minute(d)) ;
			+	BinToWord(sec(d)) ;
			+	BinToWord(0)
			
	case vartype(d) == 'X'	&& null
		s =		BinToWord(0) ;
			+	BinToWord(0) ;
			+	BinToWord(0) ;
			+	BinToWord(0) ;
			+	BinToWord(0) ;
			+	BinToWord(0) ;
			+	BinToWord(0) ;
			+	BinToWord(0)
	otherwise
		assert FALSE
		s = ''
	endcase

	return s
endfunc
*---------------------------------------------------------------------------
function BinToWord(x)

	x = int(x)
	
	return	chr(bitand(x, 0xff)) + ;
			chr(bitand(bitrshift(x,  8), 0xff))

endfunc
*----------------------------------------------------------------------------
Gregory
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform