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.

Aayushi
  • 817
  • 1
  • 7
  • 9
  • So how this doesn't work? Does it raise an exception or is formatted datetime wrong? – J.Pelttari Apr 07 '16 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 Apr 07 '16 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 Apr 07 '16 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 Apr 07 '16 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 Apr 07 '16 at 13:33
  • The problem is that when you are first extracting the `DateTime` into `strDate` you don't pass the local format settings (fmt) so your program uses the default global format settings which are different from your local one. But then when extracting `time` you expect your `strTime` to be written using your local format settings which isn't and thus your program raises an error. – SilverWarior Apr 07 '16 at 13:56
  • BTW what are you trying to achieve here. This whole ocde seems a bit unusual. – SilverWarior Apr 07 '16 at 13:57
  • You don'y have StrToDateTime(strDate) in your original program sample, you have StrToDateTime(strDate, fmt) – Dsm Apr 07 '16 at 14:36
  • Can you tell us what your trying to accomplish? Its not clear to me why you are involving strings. Are you just trying to get the relevant data from an existing date? If so, you could just use the DecodeDate and DecodeTime methods which take an existing datetime and gives you the year, month, day, hour, minute, second, millisecond. – Roy Woll Apr 07 '16 at 18:22

1 Answers1

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;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770