Impactnetworking


Borland C ++ Builder FAQ
The content The last update: 12/12/2008

Date and time (TDateTime) - the general questions

The author (): KAV, trainer
The initial link: - - -

To begin with brief information: TDateTime - as a matter of fact only a double precision floating-point number, with all that it implies. That is, for example, to calculate a difference between two dates, it is enough to subtract one of another:
TDateTime datetime1 =...; 
TDateTime datetime2 =...; 
double difference = (double) (datetime2 - datetime1);
Thus turned out value will represent exact value taking into account days, hours, minutes and seconds. For example, value 3.25 are three days and still 0.25 days (i.e. 6 hours)

How to learn leaking date and-or time?
There is nothing easier:
TDateTime CurrentDate = Date ();	//it is current date 
TDateTime CurrentTime = Time ();	//this current time 
TDateTime CurrentDateTime = Now ();//it is leaking date and time


How to shift date on the given interval?
TDateTime datetime =...; 
datetime + = 1.0 / 24;		//it is shifted on an hour forward 
datetime - = 1.0;		//to shifts per day back 
datetime + = 7.0;		//it is shifted for a week forward 
datetime = IncMonth (datetime,-1);//it is shifted on one month ago 


How to define day of week?
TDateTime datetime =...; 
int day_of_week = (datetime. DayOfWeek () + 5) % 7;//it is considered since Monday and from zero


How to define number of week in a year?
It is necessary to mean that the first week in a year can be incomplete.
int week_num; 
Word year, month, day; 
TDateTime dt =...;			//"experimental" date 
dt. DecodeDate (&year,&month,&day);	//it is decomposed our date on components 
TDateTime _1_jan (year, 1,1);		//1th January of that year to which there corresponds date 
int _1_jan_day_of_week = (_1_jan. DayOfWeek () +5) %7;//day of week on 1st of January 
week_num = (int) (dt-TDateTime (year, 1,1) + _1_jan_day_of_week)/7;//required number of week (with 0) 


How to learn, how many days in a month?
The amount of days in a month is stored in global array MonthDays, to receive value it is possible so:
MonthDays [IsLeapYear (номер_года)] [номер_месяца] 


How to change one of components TDateTime, without affecting another?
TDateTime datetime1 =...; 
TDateTime datetime2 =...; 
ReplaceDate (datetime1, datetime2);//datetime1 now contains the same date, as datetime2 
				   //but time remains former 
ReplaceTime (datetime2, datetime1);//and now datetime2 contains the same time, as datetime1 
				   //but date remained former 


How to define, whether year is leap?
For this purpose it is necessary to use function IsLeapYear. For example, the following code shows a window with the text about, whether year is leap or not:
int year = 2100; //here what it is necessary year 
ShowMessage (IntToStr (year) + "year -" + ((IsLeapYear (year))? "": "not") + "leap"); 
Apparently, function IsLeapYear accepts parameter - year and returns true, if it leap and false otherwise.

P.S. Троеточие in examples replaces some expression, allowing to write down in object correct value.