Delphi 将 TArray<Strings> 转换为 DateTime
Delphi Convert TArray<Strings> to DateTime
我想将 TArray 中包含的所有值转换为 TDateTime 类型。
ConvertDS 和 ConvertDE 是 TDateTime 变量,StoringData 是 TArray
StoringData : TArray<String>;
SetLength(StoringData,2);
for x := 0 to High(StoringData) do
for c := 0 to High(StoringData[x]) do
begin
StoringData[x] := TotTime;
StoringData[c] := DataCovertedS;
end;
ConvertDS := (StrToDateTime(StoringData[c]));
ConvertDE := (StrToDateTime(StoringData[c+1]));
数据是这样拆分的
Year := Copy(aData,0,4);
Month := Copy(aData,5,2);
Day := Copy(aData,7,2);
DataCovertedS := Concat(Year+'-'+Month+'-'+Day);
当我尝试执行它时,StrToDateTime 不起作用。
原来真正的问题是“如何从 'YYYY-MM-DD'
格式的字符串中获取 TDateTime
值?”。
幸运的是,这并不难。每次在 API 中使用新函数时,您都会阅读其文档。在这种情况下,StrToDate
文档说明如下:
S
must consist of two or three numbers, separated by the character defined by the DateSeparator
global variable or its TFormatSettings
equivalent. The order for month, day, and year is determined by the ShortDateFormat
global variable or its TFormatSettings
equivalent--possible combinations are m/d/y, d/m/y, and y/m/d.
所以,我们可以这样做:
// Define the date format:
var FS := TFormatSettings.Invariant;
FS.DateSeparator := '-';
FS.ShortDateFormat := 'y/m/d';
// Just an example of a string in this format:
const S = '2021-05-31';
// StrToDate will parse this using FS:
var D := StrToDate(S, FS);
// Test:
ShowMessage(DateToStr(D));
我想将 TArray 中包含的所有值转换为 TDateTime 类型。 ConvertDS 和 ConvertDE 是 TDateTime 变量,StoringData 是 TArray
StoringData : TArray<String>;
SetLength(StoringData,2);
for x := 0 to High(StoringData) do
for c := 0 to High(StoringData[x]) do
begin
StoringData[x] := TotTime;
StoringData[c] := DataCovertedS;
end;
ConvertDS := (StrToDateTime(StoringData[c]));
ConvertDE := (StrToDateTime(StoringData[c+1]));
数据是这样拆分的
Year := Copy(aData,0,4);
Month := Copy(aData,5,2);
Day := Copy(aData,7,2);
DataCovertedS := Concat(Year+'-'+Month+'-'+Day);
当我尝试执行它时,StrToDateTime 不起作用。
原来真正的问题是“如何从 'YYYY-MM-DD'
格式的字符串中获取 TDateTime
值?”。
幸运的是,这并不难。每次在 API 中使用新函数时,您都会阅读其文档。在这种情况下,StrToDate
文档说明如下:
S
must consist of two or three numbers, separated by the character defined by theDateSeparator
global variable or itsTFormatSettings
equivalent. The order for month, day, and year is determined by theShortDateFormat
global variable or itsTFormatSettings
equivalent--possible combinations are m/d/y, d/m/y, and y/m/d.
所以,我们可以这样做:
// Define the date format:
var FS := TFormatSettings.Invariant;
FS.DateSeparator := '-';
FS.ShortDateFormat := 'y/m/d';
// Just an example of a string in this format:
const S = '2021-05-31';
// StrToDate will parse this using FS:
var D := StrToDate(S, FS);
// Test:
ShowMessage(DateToStr(D));