Jump to content
Ian Branch

Convert two Integers to a DateTime??

Recommended Posts

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 by Ian Branch

Share this post


Link to post

You can't save duration into a datetime value, ie. you could, but that makes not much sense.

Edited by Attila Kovacs

Share this post


Link to post

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

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 by FPiette

Share this post


Link to post

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

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 by Ian Branch

Share this post


Link to post

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
Guest

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 by Guest

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×