Mega Code Archive

 
Categories / Delphi / Strings
 

Converting strings to dates

Title: Converting strings to dates Question: I want to convert strings to dates, but StrToDate doesn't do what I want. I want to convert "December 6, 1969" to a TDateTime. What should I do? Answer: StrToDate only converts numbers, so if you have month names in your string, you'll have to use VarToDateTime instead. Here are some examples: var D1, D2, D3 : TDateTime; begin D1 := VarToDateTime('December 6, 1969'); D2 := VarToDateTime('6-Apr-1998'); D3 := VarToDateTime('1998-Apr-6'); ShowMessage(DateToStr(D1)+' '+DateToStr(D2)+' '+ DateToStr(D3)); end;