強大的字串轉DateTime/Date型別函式
阿新 • • 發佈:2018-12-05
字串轉DateTime/Date型別:
支援格式樣例:
2016-2-6;16-02-06;20160206;160206;06-02-2016;
06Feb2016;6Feb2016;06FEB16;6FEB16;06-FEB-2016;6-FEB-16;
日期分隔符號支援4個: /-.\
interface
uses system.RegularExpressionsCore,system.StrUtils;
//字串轉DateTime型別
Function myStrToDateTime(sdate: String; defaultDate: TDateTime = 0): TDateTime;
//字串轉Date型別
Function myStrToDate(sdate: String; defaultDate: TDate = 0): TDate;
//英文字母月份轉換為對應數字月份.
function ReplaceMonStr(Adate: String): String;
// 擷取符合正則表示式的字串函式.
function MyPerlRegString(srcstr, pattern: String): String;
// 判斷字串是否符合正則表示式的函式.
function MyPerlRegBool(srcstr, pattern: String): boolean;
// 替換符合正則表示式的字串函式.
function MyPerlRegReplace(srcstr, pattern, newstr: String): String;
implementation
Function myStrToDateTime(sdate: String; defaultDate: TDateTime = 0): TDateTime;
var
Result := 0;
sdate := ReplaceStr(sdate, '.', '-');
sdate := ReplaceStr(sdate, '/', '-');
sdate := ReplaceStr(sdate, '\', '-');
sdate := ReplaceText(sdate, '年', '-');
sdate := ReplaceText(sdate, '月', '-');
sdate := ReplaceText(sdate, '日', '-');
// 日期部分預設格式:yyyy-MM-dd.
MyFormatSettings.ShortDateFormat := 'yyyy-MM-dd';
MyFormatSettings.LongDateFormat := 'yyyy-MM-dd';
// 日期部分是ddMMMyyyy格式,轉換為dd-MM-yyyy.
if MyPerlRegBool(sdate, '^\d{1,2}[\-]?[A-Z]{3,9}[\-]?\d{4}\b') then
begin
sdate := ReplaceMonStr(sdate);
MyFormatSettings.ShortDateFormat := 'dd-MM-yyyy';
MyFormatSettings.LongDateFormat := 'dd-MM-yyyy';
end
// 日期部分是ddmmmyy格式,轉換為dd-MM-yy格式.
else if MyPerlRegBool(sdate, '^\d{1,2}[\-]?[A-Z]{3,9}[\-]?\d{2}\b') then
begin
sdate := ReplaceMonStr(sdate);
MyFormatSettings.ShortDateFormat := 'dd-MM-yy';
MyFormatSettings.LongDateFormat := 'dd-MM-yy';
end
// 日期部分是yyyymmmdd格式,轉換為YYYY-MM-dd格式.
else if MyPerlRegBool(sdate, '^\d{8}\b') then
begin
dtPart := Copy(sdate, 1, 4) + '-' + Copy(sdate, 5, 2) + '-' +
Copy(sdate, 7, 2);
sdate := MyPerlRegReplaceAll(sdate, '^\d{8}\b', dtPart);
MyFormatSettings.ShortDateFormat := 'yyyy-MM-dd';
MyFormatSettings.LongDateFormat := 'yyyy-MM-dd';
end
// 日期部分是yymmmdd格式,yy-MM-dd格式.
else if MyPerlRegBool(sdate, '^\d{6}\b') then
begin
dtPart := Copy(sdate, 1, 2) + '-' + Copy(sdate, 3, 2) + '-' +
Copy(sdate, 5, 2);
sdate := MyPerlRegReplaceAll(sdate, '^\d{6}\b', dtPart);
MyFormatSettings.ShortDateFormat := 'yy-MM-dd';
MyFormatSettings.LongDateFormat := 'yy-MM-dd';
end
// 日期部分是dd-MM-yyyy格式.
else if MyPerlRegBool(sdate, '^\d{1,2}[\-]\d{1,2}[\-]\d{4}\b') then
begin
MyFormatSettings.ShortDateFormat := 'dd-MM-yyyy';
MyFormatSettings.LongDateFormat := 'dd-MM-yyyy';
end
// 格式:yyMMddHHnn.
else if MyPerlRegBool(sdate, '^\d{10}\b') then
begin
sdate := Copy(sdate, 1, 2) + '-' + Copy(sdate, 3, 2) + '-' +
Copy(sdate, 5, 2) + ' ' + Copy(sdate, 7, 2) + ':' + Copy(sdate, 9, 2);
MyFormatSettings.ShortDateFormat := 'yy-MM-dd HH:nn';
MyFormatSettings.LongDateFormat := 'yy-MM-dd HH:nn';
end;
Result := StrToDateTimeDef(sdate, defaultDate, MyFormatSettings);
end;
Function myStrToDate(sdate: String; defaultDate: TDate = 0): TDate;
begin
Result := 0;
sdate := ReplaceStr(sdate, '.', '-');
sdate := ReplaceStr(sdate, '/', '-');
sdate := ReplaceStr(sdate, '\', '-');
sdate := ReplaceStr(sdate, '年', '-');
sdate := ReplaceStr(sdate, '月', '-');
sdate := ReplaceStr(sdate, '日', '-');
// 日期部分預設格式:yyyy-MM-dd.
MyFormatSettings.ShortDateFormat := 'yyyy-MM-dd';
MyFormatSettings.LongDateFormat := 'yyyy-MM-dd';
// 日期部分是ddMonyyyy格式,轉換為YYYY-MM-dd格式.
if MyPerlRegBool(sdate, '^\d{1,2}[\-]?[A-Z]{3,9}[\-]?\d{4}\b') then
begin
sdate := MyPerlRegString(sdate, '^\d{1,2}[\-]?[A-Z]{3,9}[\-]?\d{4}\b');
sdate := ReplaceMonStr(sdate);
MyFormatSettings.ShortDateFormat := 'dd-MM-yyyy';
MyFormatSettings.LongDateFormat := 'dd-MM-yyyy';
end
// 日期部分是ddMonYY格式,轉換為YYYY-MM-dd格式.
else if MyPerlRegBool(sdate, '^\d{1,2}[\-]?[A-Z]{3,9}[\-]?\d{2}\b') then
begin
sdate := MyPerlRegString(sdate, '^\d{1,2}[\-]?[A-Z]{3,9}[\-]?\d{2}\b');
sdate := ReplaceMonStr(sdate);
MyFormatSettings.ShortDateFormat := 'dd-MM-yy';
MyFormatSettings.LongDateFormat := 'dd-MM-yy';
end
// 日期部分是yyyymmdd格式,轉換為YYYY-MM-dd格式.
else if MyPerlRegBool(sdate, '^\d{8}\b') then
begin
sdate := Copy(sdate, 1, 4) + '-' + Copy(sdate, 5, 2) + '-' +
Copy(sdate, 7, 2);
MyFormatSettings.ShortDateFormat := 'yyyy-MM-dd';
MyFormatSettings.LongDateFormat := 'yyyy-MM-dd';
end
// 日期部分是yymmdd格式,轉換為YYYY-MM-dd格式.
else if MyPerlRegBool(sdate, '^\d{6}\b') then
begin
sdate := Copy(sdate, 1, 2) + '-' + Copy(sdate, 3, 2) + '-' +
Copy(sdate, 5, 2);
MyFormatSettings.ShortDateFormat := 'yy-MM-dd';
MyFormatSettings.LongDateFormat := 'yy-MM-dd';
end
// 日期部分是dd-MM-yyyy格式.
else if MyPerlRegBool(sdate, '^\d{1,2}[\-]\d{1,2}[\-]\d{4}\b') then
begin
sdate := MyPerlRegString(sdate, '^\d{1,2}[\-]\d{1,2}[\-]\d{4}\b');
MyFormatSettings.ShortDateFormat := 'dd-MM-yyyy';
MyFormatSettings.LongDateFormat := 'dd-MM-yyyy';
end;
Result := StrToDateDef(sdate, defaultDate, MyFormatSettings);
end;
function ReplaceMonStr(Adate: String): String;
begin
with TPerlRegEx.Create do
begin
Subject := Adate;
Options := [preCaseLess, preMultiLine, preExtended];
if ContainsText(Adate, 'Jan') then
begin
RegEx := '[/\-\.\\]?Jan[uary]{0,4}[/\-\.\\]?';
Replacement := '-01-';
end
else if ContainsText(Adate, 'Feb') then
begin
RegEx := '[/\-\.\\]?Feb[ruary]{0,5}[/\-\.\\]?';
Replacement := '-02-';
end
else if ContainsText(Adate, 'Mar') then
begin
RegEx := '[/\-\.\\]?Mar[ch]{0,2}[/\-\.\\]?';
Replacement := '-03-';
end
else if ContainsText(Adate, 'Apr') then
begin
RegEx := '[/\-\.\\]?Apr[il]{0,2}[/\-\.\\]?';
Replacement := '-04-';
end
else if ContainsText(Adate, 'May') then
begin
RegEx := '[/\-\.\\]?May[/\-\.\\]?';
Replacement := '-05-';
end
else if ContainsText(Adate, 'Jun') then
begin
RegEx := '[/\-\.\\]?(Jun|June)[/\-\.\\]?';
Replacement := '-06-';
end
else if ContainsText(Adate, 'Jul') then
begin
RegEx := '[/\-\.\\]?(Jul|July)[/\-\.\\]?';
Replacement := '-07-';
end
else if ContainsText(Adate, 'Aug') then
begin
RegEx := '[/\-\.\\]?Aug[ust]{0,3}[/\-\.\\]?';
Replacement := '-08-';
end
else if ContainsText(Adate, 'Sep') then
begin
RegEx := '[/\-\.\\]?Sep[tember]{0,6}[/\-\.\\]?';
Replacement := '-09-';
end
else if ContainsText(Adate, 'Oct') then
begin
RegEx := '[/\-\.\\]?Oct[ober]{0,4}[/\-\.\\]?';
Replacement := '-10-';
end
else if ContainsText(Adate, 'Nov') then
begin
RegEx := '[/\-\.\\]?Nov[ember]{0,5}[/\-\.\\]?';
Replacement := '-11-';
end
else if ContainsText(Adate, 'Dec') then
begin
RegEx := '[/\-\.\\]?Dec[ember]{0,5}[/\-\.\\]?';
Replacement := '-12-';
end;
if Match then
ReplaceAll;
Result := Subject;
Free;
end;
end;
// 擷取符合正則表示式的字串函式.
Function MyPerlRegString(srcstr, pattern: String): String;
var
reg: TPerlRegEx; // 宣告正則表示式變數
begin
reg := TPerlRegEx.Create; // 建立
try
reg.Subject := srcstr; // 這是源字串
reg.Options := [preCaseLess, preMultiLine];
reg.RegEx := pattern;
if reg.Match then
Result := reg.MatchedText
else
Result := '';
finally
FreeAndNil(reg); // 或 reg.Free
end;
end;
// 判斷字串是否符合正則表示式的函式.
Function MyPerlRegBool(srcstr, pattern: String): boolean;
var
reg: TPerlRegEx; // 宣告正則表示式變數
begin
reg := TPerlRegEx.Create; // 建立
try
reg.Subject := srcstr; // 這是源字串
reg.Options := [preCaseLess, preMultiLine];
reg.RegEx := pattern;
Result := reg.Match;
finally
FreeAndNil(reg); // 或 reg.Free
end;
end;
Function MyPerlRegReplace(srcstr, pattern, newstr: String): String;
var
reg: TPerlRegEx; // 宣告正則表示式變數
begin
reg := TPerlRegEx.Create; // 建立
try
reg.Subject := srcstr; // 這是源字串
reg.Options := [preCaseLess, preMultiLine, preExtended];
reg.RegEx := pattern;
reg.Replacement := newstr;
if reg.Match then
reg.Replace;
Result := reg.Subject;
finally
FreeAndNil(reg); // 或 reg.Free
end;
end;
支援格式樣例:
2016-2-6;16-02-06;20160206;160206;06-02-2016;
06Feb2016;6Feb2016;06FEB16;6FEB16;06-FEB-2016;6-FEB-16;
日期分隔符號支援4個: /-.\
interface
uses system.RegularExpressionsCore,system.StrUtils;
//字串轉DateTime型別
Function myStrToDateTime(sdate: String; defaultDate: TDateTime = 0): TDateTime;
//字串轉Date型別
Function myStrToDate(sdate: String; defaultDate: TDate = 0): TDate;
//英文字母月份轉換為對應數字月份.
function ReplaceMonStr(Adate: String): String;
// 擷取符合正則表示式的字串函式.
function MyPerlRegString(srcstr, pattern: String): String;
// 判斷字串是否符合正則表示式的函式.
function MyPerlRegBool(srcstr, pattern: String): boolean;
// 替換符合正則表示式的字串函式.
function MyPerlRegReplace(srcstr, pattern, newstr: String): String;
implementation
Function myStrToDateTime(sdate: String; defaultDate: TDateTime
var
dtPart: String;
MyFormatSettings : TFormatSettings;
beginResult := 0;
sdate := ReplaceStr(sdate, '.', '-');
sdate := ReplaceStr(sdate, '/', '-');
sdate := ReplaceStr(sdate, '\', '-');
sdate := ReplaceText(sdate, '年', '-');
sdate := ReplaceText(sdate, '月', '-');
sdate := ReplaceText(sdate, '日', '-');
// 日期部分預設格式:yyyy-MM-dd.
MyFormatSettings.ShortDateFormat := 'yyyy-MM-dd';
MyFormatSettings.LongDateFormat := 'yyyy-MM-dd';
// 日期部分是ddMMMyyyy格式,轉換為dd-MM-yyyy.
if MyPerlRegBool(sdate, '^\d{1,2}[\-]?[A-Z]{3,9}[\-]?\d{4}\b') then
begin
sdate := ReplaceMonStr(sdate);
MyFormatSettings.ShortDateFormat := 'dd-MM-yyyy';
MyFormatSettings.LongDateFormat := 'dd-MM-yyyy';
end
// 日期部分是ddmmmyy格式,轉換為dd-MM-yy格式.
else if MyPerlRegBool(sdate, '^\d{1,2}[\-]?[A-Z]{3,9}[\-]?\d{2}\b') then
begin
sdate := ReplaceMonStr(sdate);
MyFormatSettings.ShortDateFormat := 'dd-MM-yy';
MyFormatSettings.LongDateFormat := 'dd-MM-yy';
end
// 日期部分是yyyymmmdd格式,轉換為YYYY-MM-dd格式.
else if MyPerlRegBool(sdate, '^\d{8}\b') then
begin
dtPart := Copy(sdate, 1, 4) + '-' + Copy(sdate, 5, 2) + '-' +
Copy(sdate, 7, 2);
sdate := MyPerlRegReplaceAll(sdate, '^\d{8}\b', dtPart);
MyFormatSettings.ShortDateFormat := 'yyyy-MM-dd';
MyFormatSettings.LongDateFormat := 'yyyy-MM-dd';
end
// 日期部分是yymmmdd格式,yy-MM-dd格式.
else if MyPerlRegBool(sdate, '^\d{6}\b') then
begin
dtPart := Copy(sdate, 1, 2) + '-' + Copy(sdate, 3, 2) + '-' +
Copy(sdate, 5, 2);
sdate := MyPerlRegReplaceAll(sdate, '^\d{6}\b', dtPart);
MyFormatSettings.ShortDateFormat := 'yy-MM-dd';
MyFormatSettings.LongDateFormat := 'yy-MM-dd';
end
// 日期部分是dd-MM-yyyy格式.
else if MyPerlRegBool(sdate, '^\d{1,2}[\-]\d{1,2}[\-]\d{4}\b') then
begin
MyFormatSettings.ShortDateFormat := 'dd-MM-yyyy';
MyFormatSettings.LongDateFormat := 'dd-MM-yyyy';
end
// 格式:yyMMddHHnn.
else if MyPerlRegBool(sdate, '^\d{10}\b') then
begin
sdate := Copy(sdate, 1, 2) + '-' + Copy(sdate, 3, 2) + '-' +
Copy(sdate, 5, 2) + ' ' + Copy(sdate, 7, 2) + ':' + Copy(sdate, 9, 2);
MyFormatSettings.ShortDateFormat := 'yy-MM-dd HH:nn';
MyFormatSettings.LongDateFormat := 'yy-MM-dd HH:nn';
end;
Result := StrToDateTimeDef(sdate, defaultDate, MyFormatSettings);
end;
Function myStrToDate(sdate: String; defaultDate: TDate = 0): TDate;
begin
Result := 0;
sdate := ReplaceStr(sdate, '.', '-');
sdate := ReplaceStr(sdate, '/', '-');
sdate := ReplaceStr(sdate, '\', '-');
sdate := ReplaceStr(sdate, '年', '-');
sdate := ReplaceStr(sdate, '月', '-');
sdate := ReplaceStr(sdate, '日', '-');
// 日期部分預設格式:yyyy-MM-dd.
MyFormatSettings.ShortDateFormat := 'yyyy-MM-dd';
MyFormatSettings.LongDateFormat := 'yyyy-MM-dd';
// 日期部分是ddMonyyyy格式,轉換為YYYY-MM-dd格式.
if MyPerlRegBool(sdate, '^\d{1,2}[\-]?[A-Z]{3,9}[\-]?\d{4}\b') then
begin
sdate := MyPerlRegString(sdate, '^\d{1,2}[\-]?[A-Z]{3,9}[\-]?\d{4}\b');
sdate := ReplaceMonStr(sdate);
MyFormatSettings.ShortDateFormat := 'dd-MM-yyyy';
MyFormatSettings.LongDateFormat := 'dd-MM-yyyy';
end
// 日期部分是ddMonYY格式,轉換為YYYY-MM-dd格式.
else if MyPerlRegBool(sdate, '^\d{1,2}[\-]?[A-Z]{3,9}[\-]?\d{2}\b') then
begin
sdate := MyPerlRegString(sdate, '^\d{1,2}[\-]?[A-Z]{3,9}[\-]?\d{2}\b');
sdate := ReplaceMonStr(sdate);
MyFormatSettings.ShortDateFormat := 'dd-MM-yy';
MyFormatSettings.LongDateFormat := 'dd-MM-yy';
end
// 日期部分是yyyymmdd格式,轉換為YYYY-MM-dd格式.
else if MyPerlRegBool(sdate, '^\d{8}\b') then
begin
sdate := Copy(sdate, 1, 4) + '-' + Copy(sdate, 5, 2) + '-' +
Copy(sdate, 7, 2);
MyFormatSettings.ShortDateFormat := 'yyyy-MM-dd';
MyFormatSettings.LongDateFormat := 'yyyy-MM-dd';
end
// 日期部分是yymmdd格式,轉換為YYYY-MM-dd格式.
else if MyPerlRegBool(sdate, '^\d{6}\b') then
begin
sdate := Copy(sdate, 1, 2) + '-' + Copy(sdate, 3, 2) + '-' +
Copy(sdate, 5, 2);
MyFormatSettings.ShortDateFormat := 'yy-MM-dd';
MyFormatSettings.LongDateFormat := 'yy-MM-dd';
end
// 日期部分是dd-MM-yyyy格式.
else if MyPerlRegBool(sdate, '^\d{1,2}[\-]\d{1,2}[\-]\d{4}\b') then
begin
sdate := MyPerlRegString(sdate, '^\d{1,2}[\-]\d{1,2}[\-]\d{4}\b');
MyFormatSettings.ShortDateFormat := 'dd-MM-yyyy';
MyFormatSettings.LongDateFormat := 'dd-MM-yyyy';
end;
Result := StrToDateDef(sdate, defaultDate, MyFormatSettings);
end;
function ReplaceMonStr(Adate: String): String;
begin
with TPerlRegEx.Create do
begin
Subject := Adate;
Options := [preCaseLess, preMultiLine, preExtended];
if ContainsText(Adate, 'Jan') then
begin
RegEx := '[/\-\.\\]?Jan[uary]{0,4}[/\-\.\\]?';
Replacement := '-01-';
end
else if ContainsText(Adate, 'Feb') then
begin
RegEx := '[/\-\.\\]?Feb[ruary]{0,5}[/\-\.\\]?';
Replacement := '-02-';
end
else if ContainsText(Adate, 'Mar') then
begin
RegEx := '[/\-\.\\]?Mar[ch]{0,2}[/\-\.\\]?';
Replacement := '-03-';
end
else if ContainsText(Adate, 'Apr') then
begin
RegEx := '[/\-\.\\]?Apr[il]{0,2}[/\-\.\\]?';
Replacement := '-04-';
end
else if ContainsText(Adate, 'May') then
begin
RegEx := '[/\-\.\\]?May[/\-\.\\]?';
Replacement := '-05-';
end
else if ContainsText(Adate, 'Jun') then
begin
RegEx := '[/\-\.\\]?(Jun|June)[/\-\.\\]?';
Replacement := '-06-';
end
else if ContainsText(Adate, 'Jul') then
begin
RegEx := '[/\-\.\\]?(Jul|July)[/\-\.\\]?';
Replacement := '-07-';
end
else if ContainsText(Adate, 'Aug') then
begin
RegEx := '[/\-\.\\]?Aug[ust]{0,3}[/\-\.\\]?';
Replacement := '-08-';
end
else if ContainsText(Adate, 'Sep') then
begin
RegEx := '[/\-\.\\]?Sep[tember]{0,6}[/\-\.\\]?';
Replacement := '-09-';
end
else if ContainsText(Adate, 'Oct') then
begin
RegEx := '[/\-\.\\]?Oct[ober]{0,4}[/\-\.\\]?';
Replacement := '-10-';
end
else if ContainsText(Adate, 'Nov') then
begin
RegEx := '[/\-\.\\]?Nov[ember]{0,5}[/\-\.\\]?';
Replacement := '-11-';
end
else if ContainsText(Adate, 'Dec') then
begin
RegEx := '[/\-\.\\]?Dec[ember]{0,5}[/\-\.\\]?';
Replacement := '-12-';
end;
if Match then
ReplaceAll;
Result := Subject;
Free;
end;
end;
// 擷取符合正則表示式的字串函式.
Function MyPerlRegString(srcstr, pattern: String): String;
var
reg: TPerlRegEx; // 宣告正則表示式變數
begin
reg := TPerlRegEx.Create; // 建立
try
reg.Subject := srcstr; // 這是源字串
reg.Options := [preCaseLess, preMultiLine];
reg.RegEx := pattern;
if reg.Match then
Result := reg.MatchedText
else
Result := '';
finally
FreeAndNil(reg); // 或 reg.Free
end;
end;
// 判斷字串是否符合正則表示式的函式.
Function MyPerlRegBool(srcstr, pattern: String): boolean;
var
reg: TPerlRegEx; // 宣告正則表示式變數
begin
reg := TPerlRegEx.Create; // 建立
try
reg.Subject := srcstr; // 這是源字串
reg.Options := [preCaseLess, preMultiLine];
reg.RegEx := pattern;
Result := reg.Match;
finally
FreeAndNil(reg); // 或 reg.Free
end;
end;
Function MyPerlRegReplace(srcstr, pattern, newstr: String): String;
var
reg: TPerlRegEx; // 宣告正則表示式變數
begin
reg := TPerlRegEx.Create; // 建立
try
reg.Subject := srcstr; // 這是源字串
reg.Options := [preCaseLess, preMultiLine, preExtended];
reg.RegEx := pattern;
reg.Replacement := newstr;
if reg.Match then
reg.Replace;
Result := reg.Subject;
finally
FreeAndNil(reg); // 或 reg.Free
end;
end;