0

I am having below code which is working fine on my system as my date time format of the system is dd-mm-yyyy, but below code does not work where date time format of system is 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;

the same code with format dd/mm/yyyy does not work on my system. Please help me.

9
  • So how this doesn't work? Does it raise an exception or is formatted datetime wrong?
    – J.Pelttari
    Commented Apr 7, 2016 at 11:44
  • The question is ambiguous. You say 'the same code with dd/mm/yyyy does not work'. dd/mm/yyyy where? Please show non-working code too so we can see what you mean. Also, as J.Pelttari says, explain what you mean by 'does not work'.
    – Dsm
    Commented Apr 7, 2016 at 12:25
  • does not work on my system is not a useful problem description unless you tell us specifically how it does not work. FormatDateTime works perfectly well with the code you've posted if you properly assign your date format to ShortDateFormat instead of LongDateFormat. For the date you provided, it produces 07-04-2016 12:25:56.64.
    – Ken White
    Commented Apr 7, 2016 at 12:32
  • My problem is that if I run above code on system calender format "'dd-mm-yyyy'', it will works fine. But if for example my system DateTime format is "M/d/yy", above code will give exception on below line time := StrToDateTime(strDate) and exception is as follows '07-04-2016 12:25:56.64' is not a valid date and time'.
    – Aayushi
    Commented Apr 7, 2016 at 13:28
  • I can not use VarToDateTime as varToDateTime function is not giving time in milisecond. If I am passing string haveing time upto miliseconds it is again giving exception.
    – Aayushi
    Commented Apr 7, 2016 at 13:33

1 Answer 1

1

Your code is using LongDateFormat and LongTimeFormat, but StrToDateTime() does not use those values.

StrToDate() and StrToDateTime() use ShortDateFormat (and TwoDigitYearCenturyWindow, which does not apply in this case) to parse dates.

StrToTime() and StrToDateTime() use hard-coded logic to parse times. You cannot specify the order/presence of the hour/minute/second/millisecond values, you can only specify the TimeSeparator, DecimalSeparator, TimeAMString, and TimePMString values.

Try this instead:

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;

Not the answer you're looking for? Browse other questions tagged or ask your own question.