Ian Branch 127 Posted February 9, 2021 (edited) Hi Team, Given two Integers, one recording hours, the other minutes, how do I then convert them into a single datetime type value for saving to a DateTime field in a table.? The hours integer value can be anything from 0 to 999. Minutes 0 to 59. I need to be able to reverse that as well. i.e. take the db datetime field and convert it to the two integer values for display. Regards & TIA, Ian Edited February 9, 2021 by Ian Branch Share this post Link to post
Attila Kovacs 629 Posted February 9, 2021 (edited) You can't save duration into a datetime value, ie. you could, but that makes not much sense. Edited February 9, 2021 by Attila Kovacs Share this post Link to post
Ian Branch 127 Posted February 9, 2021 Oooops. You are correct, it is a Decimal field. Apologies, had Time on my mind and, well, call it a Senior's Moment. 😞 Ian Share this post Link to post
Lajos Juhász 293 Posted February 9, 2021 If you have enough decimals you can do: Hours + minutes / 60 Share this post Link to post
FPiette 383 Posted February 9, 2021 (edited) TDateTime is a floating point number counting days since 31/12/1899. If you want to convert a number of hours and minutes to a TDateTime (This will not really be a date and a time but a duration), the unit DateUtils has some interesting constants: OneMinute and OneHour (among others). Using those constants, you can convert like this: var Duration : TDateTime; begin Duration := OneHour * Hours + OneMinute * Minutes; Edited February 9, 2021 by FPiette Share this post Link to post
Attila Kovacs 629 Posted February 9, 2021 Keep in mind that DBMSes might store DateTime in different formats, so if you need to use those values on SQL Level chose an own format. Share this post Link to post
Ian Branch 127 Posted February 9, 2021 (edited) All. The DB Table field I need to work to/from is a Decimal field. It is an existing field that a DBComponent is accessing but is giving issues. I want to eliminate that component and use two Integer inputs and convert them to the Decimal field. 2hrs & 30 mins converts to 2.5, 55 hrs & 45mins converts to 55.75, etc. Edited February 9, 2021 by Ian Branch Share this post Link to post
Ian Branch 127 Posted February 9, 2021 Hmmm. Having written as I have above it is actually quite easy. Please ignore my Query/Question. Regards, Ian Share this post Link to post
Attila Kovacs 629 Posted February 9, 2021 It is then like Lajos said. And it will also be compatible with the faulty component. Share this post Link to post
Guest Posted February 9, 2021 (edited) a type DATETIME is in fact a "interger.fractional" value. where, integer part is a "days", and fractional part is a millisseconds. for that u can do any calculate like any other numeric values. the restrictions is in the range accept for this type, of course. for manipulate this type, you can use DateUtils unit, in a specific functions to: decode, encode, validate, etc... you dont needs so much calculates. you need just know what you want do it! you needs encode values to datetime or only date or only time, not problem. DateUtils have a function for that. you need validate a value for know if is a datetime valid, not problem, DateUtils have it. take a look. hug Edited February 9, 2021 by Guest Share this post Link to post