Delphi. I need to convert a datetime string to a TDateTime type. The code I use:
...
var
fs : TFormatSettings;
dt: TDateTime;
begin
fs := TFormatSettings.Create;
fs.DateSeparator := '-';
fs.TimeSeparator := ':';
fs.ShortDateFormat := 'dd-mmm-yy';
fs.ShortTimeFormat := 'hh:nn:ss';
dt := StrToDateTime(Timestamp, fs);
...
The string is like this: Timestamp := '26-Feb-16 08:30:00'
I get only convert error messages
EConvertError, '26-Feb-16 08:30:28' is not a valid date and time
If I manually enter a timestamp of format 'yyyy/mm/dd hh:nn:ss' and make ShortDateFormat := 'yyyy/mm/dd'; and ShortTimeFormat := 'hh:nn:ss'; I have no problems...
I don't know what I'm missing? Anyone have a clue?
StrToDateTime
does not support formats that specify the month as a name, either short or long form.
You will have to parse your text using some other method. Frankly, this format is very easy to parse.
-
to get day, month and year parts. Search through the 12 short month names to find a match. :
to find hours, minutes and seconds. Or use StrToTime
. Or you could take the input and replace short month names with month numbers and use StrToDateTime
with the mm
month format.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.