Thursday, March 17, 2011

How to split a time in hours, minutes and seconds

Just like we can split a date into the year, month and day part, we can do something similar for a time.  We can split the time into hours, minutes and seconds.  And the nice part: we can use the same tool for this as we use for splitting a date.  We can use the DateTimeUtil class.

Here's a short example:

static void GetMyTimeSplit(Args _args)
{   
    TransDateTime   myDateTime=DateTimeUtil::applyTimeZoneOffset(DateTimeUtil::getSystemDateTime(),Timezone::GMTPLUS0100BRUSSELS_COPENHAGEN_MADRID);
    int             hours;
    int             minutes;
    int             seconds;
    ;
    info(datetime2str(myDateTime));
    
    hours=DateTimeUtil::hour(myDateTime);
    minutes=DateTimeUtil::minute(myDateTime);
    seconds=DateTimeUtil::second(myDateTime);
    info(strfmt('Hours %1 - Minutes %2 - Seconds %3',int2str(hours),int2str(minutes),int2str(seconds)));
}

The DateTimeUtil class is like a kind of Swiss army knife when it comes to date and time handling.
As you see from the example above, we use it to populate our TransDateTime variable with the current date and time also.


1 comment: