获取系统日期格式(50分)

  • 主题发起人 主题发起人 南宫慕容
  • 开始时间 开始时间

南宫慕容

Unregistered / Unconfirmed
GUEST, unregistred user!
设置系统日期格式可用ShortTimeFormat和LongTimeFormat
但是如何获取系统当前的日期格式
我知道有一个API函数GetDateTime,但是不知道如何使用,请赐教!
 
=date就可以了
 
风:
; ;请说具体些,最好有一个例子
 
使用API GetDateFormat

int GetDateFormat(

; ; LCID Locale, // locale for which date is to be formatted
; ; DWORD dwFlags, // flags specifying function options
; ; CONST SYSTEMTIME *lpDate, // date to be formatted
; ; LPCTSTR lpFormat, // date format string
; ; LPTSTR lpDateStr, // buffer for storing formatted string
; ; int cchDate // size of buffer
; ;);
;

Parameters

Locale

Specifies the locale for which the date string is to be formatted. If lpFormat is NULL, the function formats the string according to the date format for this locale. If lpFormat is not NULL, the function uses the locale only for information not specified in the format picture string (for example, the locale's day and month names).
This parameter can be a locale identifier created by the MAKELCID macro, or one of the following predefined values:

LOCALE_SYSTEM_DEFAULT Default system locale.
LOCALE_USER_DEFAULT Default user locale.
;

dwFlags

A set of bit flags that specify various function options. If lpFormat is non-NULL, this parameter must be zero.
If lpFormat is NULL, you can specify a combination of the following flags:

Flag Meaning
LOCALE_NOUSEROVERRIDE If set, the function formats the string using the system default date format for the specified locale. If not set, the function formats the string using any user overrides to the locale's default date format.
DATE_SHORTDATE Use the short date format. This is the default. Cannot be used with DATE_LONGDATE.
DATE_LONGDATE Use the long date format. Cannot be used with DATE_SHORTDATE.
DATE_USE_ALT_CALENDAR Use the alternate calendar, if one exists, to format the date string. If this flag is set, the function uses the default format for that alternate calendar, rather than using any user overrides. The user overrides will be used only in the event that there is no default format for the specified alternate calendar.
;

lpDate

Pointer to a SYSTEMTIME structure that contains the date information to be formatted. If this pointer is NULL, the function uses the current local system date.

lpFormat

Pointer to a format picture string to use to form the date string. If lpFormat is NULL, the function uses the date format of the specified locale.
Use the following elements to construct a format picture string. If you use spaces to separate the elements in the format string, these spaces will appear in the same location in the output string. The letters must be in uppercase or lowercase as shown in the table (for example, "MM" not "mm"). Characters in the format string that are enclosed in single quotation marks will appear in the same location and unchanged in the output string.

Picture Meaning
d Day of month as digits with no leading zero for single-digit days.
dd Day of month as digits with leading zero for single-digit days.
ddd Day of week as a three-letter abbreviation. The function uses the LOCALE_SABBREVDAYNAME value associated with the specified locale.
dddd Day of week as its full name. The function uses the LOCALE_SDAYNAME value associated with the specified locale.
M Month as digits with no leading zero for single-digit months.
MM Month as digits with leading zero for single-digit months.
MMM Month as a three-letter abbreviation. The function uses the LOCALE_SABBREVMONTHNAME value associated with the specified locale.
MMMM Month as its full name. The function uses the LOCALE_SMONTHNAME value associated with the specified locale.
y Year as last two digits, but with no leading zero for years less than 10.
yy Year as last two digits, but with leading zero for years less than 10.
yyyy Year represented by full four digits.
gg Period/era string. The function uses the CAL_SERASTRING value associated with the specified locale. This element is ignored if the date to be formatted does not have an associated era or period string.
;

For example, to get the date string

"Wed, Aug 31 94"

use the following picture string:

"ddd',' MMM dd yy"

lpDateStr

Pointer to a buffer that receives the formatted date string.

cchDate

Specifies the size, in bytes (ANSI version) or characters (Unicode version), of the lpDateStr buffer. If cchDate is zero, the function returns the number of bytes or characters required to hold the formatted date string, and the buffer pointed to by lpDateStr is not used.

;

Return Values

If the function succeeds, the return value is the number of bytes (ANSI version) or characters (Unicode version) written to the lpDateStr buffer, or if the cchDate parameter is zero, the number of bytes or characters required to hold the formatted date string.
If the function fails, the return value is zero. To get extended error information, call GetLastError. GetLastError may return one of the following error codes:

ERROR_INSUFFICIENT_BUFFER
ERROR_INVALID_FLAGS
ERROR_INVALID_PARAMETER
 
;DateSeparator := '-';
; ShortDateFormat := 'm/d/yyyy';
; Label1.Caption := DateToStr(Date);
; delphi中有帮助的。
;
 
pcsunflower:
; ; ;对,就是这个函数,你有在Delohi里调用的例程吗?谢谢
 
; ;RegDate.RootKey := HKEY_CURRENT_USER;
; ; RegDate.OpenKey('/Control Panel/International', false);
; ; DateCfg := RegDate.ReadString('sShortDate');
; ; TimeCfg := RegDate.ReadString('sTimeFormat');
; ; if (DateCfg <> 'yyyy-MM-dd') and (DateCfg <> 'yyyy/MM/dd')
; ; ; and (dateCfg <> 'yyyy.MM.dd') or (TimeCfg <> 'HH:mm:ss') then
; ; begin
; ; ; if RegDate.OpenKey('/Control Panel/International', true) then
; ; ; begin
; ; ; ; RegDate.WriteString('sShortDate', 'yyyy-MM-dd');
; ; ; ; RegDate.WriteString('sTimeFormat', 'HH:mm:ss');
; ; ; end;
; ; ; MessageDlg('系统修正了日期格式,需重新运行本程序!!!', mtInformation, [mbYes], 0);
; ; ; halt(0);
; ; end;
 
多谢各位的大力帮助!
现在我在做数据录入,其中有某些字段是日期型的,
后台数据库的日期型格式要求为:YYYY-MM-DD。
现在我想在进入我的程序时,把系统的日期格式改为YYYY-MM-DD型,
退出我的程序时,又把原来的日期格式还原。
请各位继续讨论!
 
南宫慕容:
我的代码绝对可以用啦,
你在数据模块CREATE时运行这段代码就可以修正日期时间格式了.
 
去买张程序员大本营,上面象你这样问题的源代码一大堆!不用浪费分啦!
 
FORSS:
; ; ;我的意思是在退出时,把我改动过系统日期还原
 
那不简单,你记一下原来的,退出时再改回去不就行啦?
 
FORSS:
; ; 我就这个意思,问题是怎么“记”。所以我要获取当前的系统日期格式呀
 
>>多谢各位的大力帮助!
>>现在我在做数据录入,其中有某些字段是日期型的,
>>后台数据库的日期型格式要求为:YYYY-MM-DD。
>>现在我想在进入我的程序时,把系统的日期格式改为YYYY-MM-DD型,
>>退出我的程序时,又把原来的日期格式还原。
>>请各位继续讨论!
KAO 要获取什么当前的系统日期格式呀?
这样就OK了嘛!!!
FormatDateTime('yyyy-mm-dd',这儿是你要输入的日期);
 
dingbaosheng:
; ; 请你看清楚要求。如果不获取当前的系统日期格式的话,如何在退出时改回来呢?
 
慕容兄:
你看仔细我的代码了么?
DateCfg := RegDate.ReadString('sShortDate');
TimeCfg := RegDate.ReadString('sTimeFormat');
就是取得当前的呀.你记一下要改的时候再WRITESTRING就可以了呀..
还有为什么要改回去呢?既然格式是你需要的,而且是标准的,就不要改了吧.
; ;
 
有必要把时间改来改去的吗?怪麻烦的,如果用户不习惯这种事件表示方式,
在执行你的程序过程中又执行其他的与时间格式有关的程序,用户不是要骂你了吗?
还有,在你的畅叙执行非法关闭或其他什么异常,没有该回去,又如何面对呢?

dingbaosheng说得很明白,用Format函数将渠道的时间做一个格式化就行了,还改什么系统时间格式呢?
 
drawpharos:如果有录入时,有很多字段是关于日期型的,而且查询时又要用到to_date,
难道每一次提交纪录时,都要用FormatDateTime?而且在涉及到日期查询时,我的To_date
到底是用“yyyy-mm-dd”还是当前用户系统的日期型式?即使可以用FormatDateTime,难道
我的每一个都要用,而且还要要求其他的合作者也要用?所以我觉得用FormatDateTime
不可取。

时间关系,采用了forss的方法。十分感谢各位的大力帮助!谢谢!!!
 
接受答案了.
 
后退
顶部