在Delphi中使用特定于区域性格式的毫秒DateTime



我有下面的代码,它在我的系统上运行良好,因为我的系统日期时间格式是dd-mm-yyyy,但下面的代码在系统日期-时间格式为dd/mm/yyyy的情况下不起作用。

    try
          fmt.LongDateFormat:='dd-mm-yyyy';
          fmt.DateSeparator  :='-';
          fmt.LongTimeFormat :='hh:nn:ss.z';
          fmt.TimeSeparator  :=':'    ;
          dateTime :=42467.51801;
          strDate :=FormatDateTime('dd-mm-yyyy hh:nn:ss.z', dateTime);
          time := StrToDateTime(strDate,fmt);   
           strDate :=FormatDateTime('dd-mm-yyyy hh:nn:ss.z', time);
        ShowMessage('DateTime := ' +strDate)  ;
         except
         on e: Exception do
            ShowMessage('Exception message = '+e.Message);
end;

dd/mm/yyyy格式的相同代码在我的系统上不起作用。请帮帮我。

您的代码使用LongDateFormatLongTimeFormat,但StrToDateTime()不使用这些值。

StrToDate()StrToDateTime()使用ShortDateFormat(以及TwoDigitYearCenturyWindow,在本例中不适用)来解析日期。

CCD_ 8和CCD_ 9使用硬编码逻辑来解析时间。您不能指定小时/分钟/秒/毫秒值的顺序/存在,只能指定TimeSeparatorDecimalSeparatorTimeAMStringTimePMString值。

试试这个:

try
  fmt.ShortDateFormat := 'dd/mm/yyyy';
  fmt.DateSeparator := '/';
  fmt.TimeSeparator := ':';
  fmt.DecimalSeparator := '.';
  dateTime := 42467.51801;
  strDate := FormatDateTime('dd/mm/yyyy hh:nn:ss.z', dateTime, fmt);
  time := StrToDateTime(strDate, fmt);
  strDate := FormatDateTime('dd/mm/yyyy hh:nn:ss.z', time, fmt);
  ShowMessage('DateTime := ' + strDate);
except
  on e: Exception do
    ShowMessage('Exception message = '+e.Message);
end;

最新更新