关于时间格式转换,会者不难(100分)

  • 主题发起人 主题发起人 tiger_520
  • 开始时间 开始时间
T

tiger_520

Unregistered / Unconfirmed
GUEST, unregistred user!
大家都知道在XP系统下,区域和语言选项中的“标准和格式”中,当你选择了不同语言方式,其中的货币、时间格式均会发生不同的变化,如:短时间欧洲和美国格式为:11/3/2006,中国的时间格式为2006-11-3,如果想将美国时间11/3/2006 4:20:20 pm转换为中国的时间格式2006-11-3 16:20:20,有没有标准函数将各时间格式的标准随意转换?
 
使用 CONVERT:

CONVERT (data_type[(length)], expression [, style])

参数
expression

是任何有效的 Microsoft® SQL Server™ 表达式。有关更多信息,请参见表达式。

data_type

目标系统所提供的数据类型,包括 bigint 和 sql_variant。不能使用用户定义的数据类型。有关可用的数据类型的更多信息,请参见数据类型。

length

nchar、nvarchar、char、varchar、binary 或 varbinary 数据类型的可选参数。

style

日期格式样式,借以将 datetime 或 smalldatetime 数据转换为字符数据(nchar、nvarchar、char、varchar、nchar 或 nvarchar 数据类型);或者字符串格式样式,借以将 float、real、money 或 smallmoney 数据转换为字符数据(nchar、nvarchar、char、varchar、nchar 或 nvarchar 数据类型)。

SQL Server 支持使用科威特算法的阿拉伯样式中的数据格式。

在表中,左侧的两列表示将 datetime 或 smalldatetime 转换为字符数据的 style 值。给 style 值加 100,可获得包括世纪数位的四位年份 (yyyy)。

不带世纪数位 (yy) 带世纪数位 (yyyy)
标准
输入/输出**
- 0 或 100 (*) 默认值 mon dd yyyy hh:miAM(或 PM)
1 101 美国 mm/dd/yyyy
2 102 ANSI yy.mm.dd
3 103 英国/法国 dd/mm/yy
4 104 德国 dd.mm.yy
5 105 意大利 dd-mm-yy
6 106 - dd mon yy
7 107 - mon dd, yy
8 108 - hh:mm:ss
- 9 或 109 (*) 默认值 + 毫秒 mon dd yyyy hh:mi:ss:mmmAM(或 PM)
10 110 美国 mm-dd-yy
11 111 日本 yy/mm/dd
12 112 ISO yymmdd
- 13 或 113 (*) 欧洲默认值 + 毫秒 dd mon yyyy hh:mm:ss:mmm(24h)
14 114 - hh:mi:ss:mmm(24h)
- 20 或 120 (*) ODBC 规范 yyyy-mm-dd hh:mm:ss[.fff]
- 21 或 121 (*) ODBC 规范(带毫秒) yyyy-mm-dd hh:mm:ss[.fff]
- 126(***) ISO8601 yyyy-mm-dd Thh:mm:ss:mmm(不含空格)
- 130* 科威特 dd mon yyyy hh:mi:ss:mmmAM
- 131* 科威特 dd/mm/yy hh:mi:ss:mmmAM
 
FormatDateTime()
 
用一个函数就行了,你设置好格式串,想什么格式就什么格式
function FormatDateTime(const Format: string; DateTime: TDateTime): string;
overload;
Date-Time Format Strings specify the formatting of date-time values (such as TDateTime) when they are converted to strings. Date-Time Format Strings are passed to formatting routines (such as FormatDateTime), and are also used to set certain global variables (such as ShortDateFormat).

Date Time Format Strings are composed from specifiers that represent values to be inserted into the formatted string. Some specifiers (such as "d"), simply format numbers or strings. Other specifiers (such as "/") refer to locale-specific strings from global variables.

In the following table, specifiers are given in lower case. Case is ignored in formats, except for the "am/pm" and "a/p" specifiers.

Specifier Displays
c Displays the date using the format given by the ShortDateFormat global variable, followed by the time using the format given by the LongTimeFormat global variable. The time is not displayed if the date-time value indicates midnight precisely.
d Displays the day as a number without a leading zero (1-31).
dd Displays the day as a number with a leading zero (01-31).
ddd Displays the day as an abbreviation (Sun-Sat) using the strings given by the ShortDayNames global variable.
dddd Displays the day as a full name (Sunday-Saturday) using the strings given by the LongDayNames global variable.
ddddd Displays the date using the format given by the ShortDateFormat global variable.
dddddd Displays the date using the format given by the LongDateFormat global variable.
e (Windows only) Displays the year in the current period/era as a number without a leading zero (Japanese, Korean and Taiwanese locales only).
ee (Windows only) Displays the year in the current period/era as a number with a leading zero (Japanese, Korean and Taiwanese locales only).
g (Windows only) Displays the period/era as an abbreviation (Japanese and Taiwanese locales only).
gg (Windows only) Displays the period/era as a full name. (Japanese and Taiwanese locales only).
m Displays the month as a number without a leading zero (1-12). If the m specifier immediately follows an h or hh specifier, the minute rather than the month is displayed.
mm Displays the month as a number with a leading zero (01-12). If the mm specifier immediately follows an h or hh specifier, the minute rather than the month is displayed.
mmm Displays the month as an abbreviation (Jan-Dec) using the strings given by the ShortMonthNames global variable.
mmmm Displays the month as a full name (January-December) using the strings given by the LongMonthNames global variable.
yy Displays the year as a two-digit number (00-99).
yyyy Displays the year as a four-digit number (0000-9999).
h Displays the hour without a leading zero (0-23).
hh Displays the hour with a leading zero (00-23).
n Displays the minute without a leading zero (0-59).
nn Displays the minute with a leading zero (00-59).
s Displays the second without a leading zero (0-59).
ss Displays the second with a leading zero (00-59).
z Displays the millisecond without a leading zero (0-999).
zzz Displays the millisecond with a leading zero (000-999).
t Displays the time using the format given by the ShortTimeFormat global variable.
tt Displays the time using the format given by the LongTimeFormat global variable.
am/pm Uses the 12-hour clock for the preceding h or hh specifier, and displays 'am' for any hour before noon, and 'pm' for any hour after noon. The am/pm specifier can use lower, upper, or mixed case, and the result is displayed accordingly.
a/p Uses the 12-hour clock for the preceding h or hh specifier, and displays 'a' for any hour before noon, and 'p' for any hour after noon. The a/p specifier can use lower, upper, or mixed case, and the result is displayed accordingly.
ampm Uses the 12-hour clock for the preceding h or hh specifier, and displays the contents of the TimeAMString global variable for any hour before noon, and the contents of the TimePMString global variable for any hour after noon.
/ Displays the date separator character given by the DateSeparator global variable.
: Displays the time separator character given by the TimeSeparator global variable.
'xx'/"xx" Characters enclosed in single or double quotes are displayed as-is, and do not affect formatting.
 
danng:
从一个字符串11/3/2006 4:55:34 pm,你怎么用formatDatetime转换成2006-11-3 16:55:34?前提在你不转换控制面板的时间格式下?
 
最好是在你的应用程序里规范一下时间格式,使日期格式使用一个标准,然后就可以使用一种标准的格式了
修改系统的日期显示格式通过修改注册表来改变系统日期显示格式,也相当于在系统控制面板之区域设置里手工改动。主要是为了统一应用程序中的日期格式。
procedure SetSystemDateTimeFormat;
var
Reg : TRegistry;
begin
Reg := TRegistry.Create;
with Reg do
begin
try
RootKey := HKEY_CURRENT_USER;
if OpenKey('Control Panel/International',False) then
begin
WriteString('sDate','-');
WriteString('sShortDate','yyyy-MM-dd');
end;
RootKey := HKEY_USERS;
OpenKey('',False);
if OpenKey('.Default/Control Panel/International',False) then
begin
WriteString('sDate','-');
WriteString('sShortDate','yyyy-MM-dd');
end;
finally
Free;
end;
end;
end;
 
我说的SQL中CONVERT就可以的,很多格式都行的.以下你在查询分析器运行一下就看出效果了.具体你看帮助.
select convert(varchar(20) ,getdate() ,120)
select convert(varchar(20) ,getdate() ,110)
select convert(varchar(20) ,getdate() ,102)
select convert(varchar(20) ,getdate() ,103)
select convert(varchar(20) ,getdate() ,109)
formatdatetime 也可以的.
ShowMessage(Formatdatetime('hh-nn-ss-yyyy-mm-dd',now));
ShowMessage(Formatdatetime('yyyy-mm-dd-hh-nn-ss',now));
 
自己搞定了,谢谢各位捧场阿
 

Similar threads

后退
顶部