[SOLVED] EncodeDateTime/TDateTimePicker blues lazarus / 31-LCL ---------------------------------------------------------------------- JD · 2016-08-21 20:51 Hi there all, I have a problem with TDateTimePicker. I'm using a function to convert a SQLDateTime to DateTime Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // e.g 2016-08-22 09:35function SQLDateTimeToDateTime(const SQLDateTime: string): TDateTime;begin  Result :=      EncodeDateTime(        SysUtils.StrToInt(Copy(SQLDateTime, 1, 4)),        SysUtils.StrToInt(Copy(SQLDateTime, 6, 2)),        SysUtils.StrToInt(Copy(SQLDateTime, 9, 2)),        SysUtils.StrToInt(Copy(SQLDateTime, 12, 2)),        SysUtils.StrToInt(Copy(SQLDateTime, 15, 2)),        0, 0      );end;  When a string like 2016-08-22 09:35, when I copy it to DateTimePicker (with Kind property of dtkDateTime) using the code below, Code: [Select]  DateTimePicker1.Date := SQLDateTimeToDateTime(Trim(Edit1.Text)); I get "22/08/2016 00:00:00" as result. That is the correct date BUT the time at midnight. But when I use Code: [Select]  ShowMessage(FormatDateTime('dd/mm/yyyy hh:nn:ss', SQLDateTimeToDateTime(Trim(Edit1.Text)))); I get the correct value of "22/08/2016 09:35:00"! What can I do to correct this problem with TDateTimePicker? Thanks. ---------------------------------------------------------------------- Cyrax · 2016-08-21 21:05 Have you tried replace your SQLDateTimeToDateTime function with StrToDateTime and custom TFormatSettings values? ---------------------------------------------------------------------- Phil · 2016-08-21 21:09 You probably need to set DateTimePicker1.Time too. ---------------------------------------------------------------------- howardpc · 2016-08-21 21:12 Yes, this is a feature of TDateTimePicker. It implements both a Date and a Time property. The Date property slices off all time data, and the Time property slices off all date data. So to show both Date and Time using a TDateTimePicker you have to re- combine  the Date and Time properties. ---------------------------------------------------------------------- JD · 2016-08-21 21:40 Quote from: howardpc on August 21, 2016, 11:12:00 pmYes, this is a feature of TDateTimePicker. It implements both a Date and a Time property. The Date property slices off all time data, and the Time property slices off all date data. So to show both Date and Time using a TDateTimePicker you have to re- combine  the Date and Time properties. Thanks. How do I do this? ---------------------------------------------------------------------- Phil · 2016-08-21 21:45 Did you try this? DateTimePicker1.Date := SQLDateTimeToDateTime(Trim(Edit1.Text)); DateTimePicker1.Time := SQLDateTimeToDateTime(Trim(Edit1.Text)); ---------------------------------------------------------------------- JD · 2016-08-21 21:52 Quote from: Phil on August 21, 2016, 11:45:01 pmDid you try this? DateTimePicker1.Date := SQLDateTimeToDateTime(Trim(Edit1.Text)); DateTimePicker1.Time := SQLDateTimeToDateTime(Trim(Edit1.Text)); Ah thanks Phil. It worked! ---------------------------------------------------------------------- howardpc · 2016-08-21 21:54 Actually, we all missed that TDateTimePicker has a (public, not published) DateTime property which is probably what you want to use... ---------------------------------------------------------------------- Zoran · 2016-08-22 19:13 Quote from: howardpc on August 21, 2016, 11:54:40 pmActually, we all missed that TDateTimePicker has a (public, not published) DateTime property which is probably what you want to use... Yes, see in the wiki, it says just that QuoteThis property is provided to allow setting or reading of both date and time value at once in program code.. So, you need one line after all: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i DateTimePicker1.DateTime := SQLDateTimeToDateTime(Trim(Edit1.Text)); ---------------------------------------------------------------------- JD · 2016-08-22 20:32 Quote from: Zoran on August 22, 2016, 09:13:05 pmSo, you need one line after all: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i DateTimePicker1.DateTime := SQLDateTimeToDateTime(Trim(Edit1.Text)); That one line did not work. Phil's two line suggestion did. Why? ---------------------------------------------------------------------- Zoran · 2016-08-27 15:59 Quote from: JD on August 22, 2016, 10:32:47 pmQuote from: Zoran on August 22, 2016, 09:13:05 pmSo, you need one line after all: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i DateTimePicker1.DateTime := SQLDateTimeToDateTime(Trim(Edit1.Text)); That one line did not work. Phil's two line suggestion did. Why? Are you sure that setting DateTime property does not work??? Please notice the diferrence between 1. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i DateTimePicker1.Date := and 2. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i DateTimePicker1.DateTime := The second one replaces the two lines which Phil suggested, it sets Date and Time, whereas the first sets only Date, not time. Look your first post, you set Date property there, not DateTime: Quote from: JD on August 21, 2016, 10:51:40 pmWhen a string like 2016-08-22 09:35, when I copy it to DateTimePicker (with Kind property of dtkDateTime) using the code below, Code: [Select]  DateTimePicker1.Date := SQLDateTimeToDateTime(Trim(Edit1.Text)); I get "22/08/2016 00:00:00" as result. That is the correct date BUT the time at midnight. The answer is don't set DateTimePicker1.Date, but do set DateTimePicker1.DateTime and you will get what you expected.