Work out number of days using Month Calendar Delphi - delphi

I'm using two Month calendar on my form. I select a date on each one and I want to calculate the number of days between these two days. I only code I have is
procedure TForm1.Button1Click(Sender: TObject);
Var
N, m: TDate;
d: Real;
l,k:String;
begin
N := (MonthCalendar1.Date);
m := MonthCalendar2.Date;
L := formatdatetime('dd', N);
K:=formatdatetime('dd',M);
d := StrToFloat(L)-StrToFloat(K);
ShowMessage(FloatToStr(d));
end;

You can use DaysBetween() from System.DateUtils to obtain the difference, in days, between two TDateTime values.
See Docs: http://docwiki.embarcadero.com/Libraries/Rio/en/System.DateUtils.DaysBetween
Edit, see code:
uses System.DateUtils;
[..]
procedure TForm1.Button1Click(Sender: TObject);
var
NumDays: Integer;
begin
NumDays := DaysBetween(MonthCalendar1.Date, MonthCalendar2.Date);
ShowMessage('Days between selected dates: ' + NumDays.ToString);
end;

You don't need a MonthCalendar to do this. The part of a TDateTime to the RHS of the decimal point is effectively a "day number" so if you substract one of those from another, you find the "days between". Apply a Trunc to both to discard the time-of-day parts. See http://docwiki.embarcadero.com/Libraries/Rio/en/System.TDateTime for definition of TDateTime.
So, for example, given
var
D1,
D2 : TDateTime;
DaysBetween : Integer;
and D1 and D2 are two TDateTimes (that you could enter if you wish using a TMonthCalendar), then
DaysBetween := Trunc(D2) - Trunc(D1);
The call to the built-in Trunc function discards the fractional, time-of-day part of a TDateTime value (that is, the partr to the right of the decimal point), so that 23:59 on one day and 00:01 on the next are calculated to be one day apart. This may, or may not, be the result you want, depending on your application, which is why I have suggested calculating it yourself rather than using the built-in DaysBetween function.

Related

How to add only some days to date-time variable?

When I need to increase date 1/1/2016 by 7 days, I can simply do:
IncDay(myDate, 7); // Result 8/1/2016
What do I do, if I need to ignore some days (e.g. Saturdays), so the Result is 10/1/2016 ?
Apparently, based on the somewhat cryptic comments, you wish to increment a date by a number of days, excluding Saturday. You can do that by making use of the the DayOfTheWeek function in DateUtils. This will tell you which day of the week a specified date falls on.
So your function is something like this:
function IncExcludingSaturday(FromDate: TDateTime; IncDays: Integer): TDateTime;
begin
Assert(IncDays >= 0);
Result := FromDate;
if DayOfTheWeek(Result) = DaySaturday then
Result := IncDay(Result);
while IncDays > 0 do
begin
Result := IncDay(Result);
if DayOfTheWeek(Result) = DaySaturday then
Result := IncDay(Result);
dec(IncDays);
end;
end;
This is a rather crude way to achieve your goal. You can find more interesting ideas here: AddBusinessDays and GetBusinessDays
Now, in the question you suggest that 7 days from 01/01/2016, excluding Saturdays, takes you to 09/01/2016. But that is surely wrong since that date is a Saturday. The correct answer is surely 10/01/2016 which is a Sunday. In other words we need to skip over two Saturdays, on the 2nd and the 9th.
This adds a day to the calculation for each Saturday found in the ANumberOfDays range:
{.$DEFINE UNCLEAR_WHAT_YOU_R_ASKING}
function IncDayIgnoringSaturdays(const AValue: TDateTime; const ANumberOfDays: Integer = 1): TDateTime;
var
i, j: Integer;
begin
i := ANumberOfDays;
j := 0;
Result := AValue;
while i > 0 do begin
Result := IncDay(Result);
if DayOfTheWeek(Result) = DaySaturday then
Inc(j);
Dec(i);
end;
Result := IncDay(Result, j);
{$IFDEF UNCLEAR_WHAT_YOU_R_ASKING}
if DayOfTheWeek(Result) = DaySaturday then
Result := IncDay(Result);
{$ENDIF}
end;
begin
WriteLn(DateTimeToStr(IncDayIgnoringSaturdays(StrToDateTime('1/1/2016'), 7)));
WriteLn(DateTimeToStr(IncDayIgnoringSaturdays(StrToDateTime('1/1/2016'), 14)));
ReadLn;
end.
EDIT
The above may return a date on Saturday or not, depending on the UNCLEAR_WHAT_YOU_R_ASKING conditional define.
You've been given a couple of answers for the special case of excluding a single specific weekday. This is not particularly flexible.
You could try for implementing a function that takes a set of weekdays that are 'valid' as one of its parameters. The number of days to increment the date by is approximately AIncByDays * 7 / NoOfDaysInSet. But it gets rather tricky adjusting the result correctly for valid/invalid weekdays 'near' the start date. Even after all this complexity, you still wouldn't have a way to deal with special dates, like public holidays.
Fortunately there's different approach that's much simpler to implement and far more flexible. It's only drawback is that it's inefficient for 'large' increments.
The general approach is to increment 1 day at a time.
And on each increment check the validity of the new date.
Only if the new date is valid, reduce the increment counter by 1.
Repeat the above in a loop until the increment counter is reduced to 0.
The following uses a callback function to check the validity of each date.
type
TValidDateFunc = function (ADate: TDateTime): Boolean;
function IncValidDays(AStartDate: TDateTime; AIncBy: Integer; AIsValid: TValidDateFunc): TDateTime;
var
LIncDirection: Integer;
begin
// Support dec using negative AIncBy
if AIncBy >= 0 then
LIncDirection := 1
else
LIncDirection := -1;
Result := AStartDate;
while (AIncBy <> 0) do
begin
IncDay(Result, LIncDirection);
if (AIsValid(Result)) then
Dec(AIncBy, LIncDirection);
end;
end;
Now you can simply write whatever function you desire to determine a valid date and use it in the above function. E.g.
function DateNotSaturday(ADate: TDateTime): Boolean;
begin
Result := (DayOfTheWeek(ADate) <> DaySaturday);
end;
NewDate := IncValidDays(SomeDate, 10, DateNotSaturday);
Note that it now becomes quite easy to write a function that uses only work days that aren't public holidays. E.g.
function IsWorkDay(ADate: TDateTime): Boolean;
var
LDay, LMonth, LYear: Word;
begin
DecodeDate(ADate, LYear, LMonth, LDay);
Result := True;
Result := Result and (DayOfTheWeek(ADate) <> DaySaturday);
Result := Result and (DayOfTheWeek(ADate) <> DaySunday);
Result := Result and ((LDay <> 1) or (LMonth <> 1)); //Excludes New Years day.
...
end;
The biggest advantage of this approach is that you don't have to deal with the risk of 'double-ignoring' a date because it's both a weekend day and a public holiday.
NOTE: In recent versions of Delphi you could replace the callback function with an anonymous method.
Determine the day of the week using DayOfTheWeek function. Now you know when will be next Saturday and whether it will get inside your period. If your period is larger than one week, then you can multiple number of Saturdays in your period by a number of full weeks. If your period is larger than 7 weeks, then you will have to add one more day for each 7 weeks.

Convert Extended to Time

I need to convert extended values to time format. For example :
3.50 represents 00:03:50
62.02 represents 01:02:02
73.70 represents 01:14:10
I have tried the following function to convert the Hour and Minutes part but I don't have any idea on how to convert the Seconds' part.
function ConvertToTime(AValue: Extended): TDateTime;
begin
Result:= EncodeTime(trunc(ArticleRec.Quantity) div 60,trunc(ArticleRec.Quantity) mod 60,0,0);
end;
Thanking you in anticipation for your help.
The fractional part is obtained like this:
var
SecondsFrac: Double;
....
SecondsFrac := Frac(Value);
And then you can convert from a floating point fractional value in the range 0 to 1 to an integer in the range 0 to 100 like this:
var
Seconds: Integer;
....
Seconds := Round(SecondsFrac*100);
This is a pretty weird way to store time though. You have to deal with the fact that when Seconds >= 60 you need to increment the minutes, and decrement Seconds by 60.
I guess I'd do that by converting the time into seconds, and going from there:
function ConvertWeirdTimeFormatToSeconds(const Value: Double): Integer;
var
SecondsFrac: Double;
begin
SecondsFrac := Frac(Value);
Result := Round(SecondsFrac*100) + Trunc(Value)*60;
end;
You can then decode the seconds into distinct parts like this:
procedure DecodeSeconds(Value: Integer; out Hours, Minutes, Seconds: Integer);
begin
Seconds := Value mod 60;
Value := Value div 60;
Minutes := Value mod 60;
Value := Value div 60;
Hours := Value;
end;
Which makes me think it might be better to just store the time in an integer number seconds from midnight. It makes far more sense to use a standard format, in my view.
I see no reason to use Extended here, or indeed anywhere for that matter. It's a non-standard type that due to its strange size and consequent alignment issues tends to perform poorly. And it's only supported on x86.

Delphi TNumberbox returns single; how to get to a standard integer?

TNumberbox and TSpinEdit return values defined as type single. I want to use these values to do simple integer arithmetic, but I can't cast them successfully to the more generalized integer type, and Delphi gives me compile-time errors if I try to just use them as integers. This code, for example, fails with
"E2010 Incompatible types: 'Int64' and 'Extended'":
var
sMinutes: single;
T: TDatetime;
begin
sMinutes :=Numberbox1.value;
T :=incminute(Now,sMinutes);
All I want to do here is have the user give me a number of minutes and then increment a datetime value accordingly. Nothing I've tried enables me to use that single in this way.
What am I missing??
Just truncate the value before using it:
var
Minutes: Integer;
T: TDateTime;
begin
Minutes := Trunc(NumberBox1.Value);
T := IncMinute(Now, Minutes);
end;
Depending on your particular needs, you may need to use Round instead. It will correctly round to the nearest integer value, making sure that 1.999999999999 correctly becomes integer 2; Trunc would result in 1 instead. (Thanks to Heartware for this reminder.)
var
Minutes: Integer;
T: TDateTime;
begin
Minutes := Round(NumberBox1.Value);
T := IncMinute(Now, Minutes);
end;
Trunc and Round are in in the System unit.

insert number of days of year, months, days of month

How can I insert in a database the number of days of the year and at the same time insert in the same record the month, day of the month, and the day of the week?
This is my table:
tabela/coluna.Dias_ano(registo 1...365)
Year:=StrToInt(ano.Text);
diasano.Text:= IntToStr( DaysInAYear(Year) );
diasAno| Mes |diames |dia semana |
1 | janeiro | 1 |Segunda |
2 | janeiro | 2 | Terça |
...
365 | Dezembro | 31 | Segunda
Probably I'm missing the question but in case I'm not, you can find what you need in "DateUtils.pas". It has functions like "DayOfTheYear", "MonthOfTheYear", "DayOfTheMonth", "DayOfTheWeek" and many more. I think you're gonna store them in different fields, but there might be a probability that you don't need to store them at all; the database you're using might supply similar functionality, in that case you can construct your queries to supply the filtering/ordering you need.
edit: code for the 3rd comment below;
procedure TForm1.Button1Click(Sender: TObject);
var
Year, DaysInYear: Word;
FirstDay, i: Integer;
begin
Year := StrToInt(ano.Text);
DaysInYear := DaysInAYear(Year);
diasano.Text := IntToStr(DaysInYear);
FirstDay := Trunc(EncodeDate(Year, 1, 1));
for i := FirstDay to FirstDay + DaysInYear - 1 do begin
Planeamento.Append;
Planeamento.FieldByName('diasAno').Value := DayOfTheYear(i);
Planeamento.FieldByName('Month').Value := LongMonthNames[MonthOfTheYear(i)];
Planeamento.FieldByName('DayOfMonth').Value := DayOfTheMonth(i);
Planeamento.FieldByName('DayOfWeek').Value := LongDayNames[DayOfTheWeek(i)];
Planeamento.Post;
end;
end;
edit: With calculated fields;
For the below example the table has five columns instead of four. Let's name the first column 'Date'. This column is the only column to store data and will hold the Date (as per ldsandon's answer, since by storing the date instead of day-number, you won't have to keep track of what table represents what year, and calculations will be simpler).
The other four columns are exactly the same as in the question, except that they all are "calculated fields".
procedure TForm1.Button1Click(Sender: TObject);
var
Year, DaysInYear: Word;
FirstDay, i: Integer;
begin
Year := StrToInt(ano.Text);
DaysInYear := DaysInAYear(Year);
diasano.Text := IntToStr(DaysInYear);
FirstDay := Trunc(EncodeDate(Year, 1, 1));
for i := FirstDay to FirstDay + DaysInYear - 1 do begin
Planeamento.Append;
Planeamento.FieldByName('Date').Value := i;
Planeamento.Post;
end;
end;
procedure TForm1.PlaneamentoCalcFields(DataSet: TDataSet);
var
Date: TDateTime;
begin
Date := DataSet.FieldByName('Date').AsDateTime;
DataSet.FieldByName('diasAno').AsInteger := DayOfTheYear(Date);
DataSet.FieldByName('Month').AsString := LongMonthNames[MonthOfTheYear(Date)];
DataSet.FieldByName('DayOfMonth').AsInteger := DayOfTheMonth(Date);
DataSet.FieldByName('DayOfWeek').AsString := LongDayNames[DayOfTheWeek(Date)];
end;
You could simply calculate what value has 1/1/<year>, than check if <year> is leap or not and then with a simple for loop calculate the TDateTime value for each day (just add the day number to the January 1st value), extract the info you need with the DateUtils functions and write them to a record for each day.
But I would advise you agains such a solution. Those all are informations already encoded into a datetime value. I would simply store each item with its date, the whole calendar can be easily built client side when needed (and only the needed parts), without having to store it wholly in the database.

Change today's date, advancing one month and setting the systemtime

I would like a code sample for a function that takes a tDateTime and an integer as input and sets the system time using setlocaltime after advancing that tDateTime by (int) months. The time should stay the same.
pseudo code example
SetNewTime(NOW,2);
The issues I'm running into are rather frustrating. I cannot use incmonth or similar with a tDateTime, only a tDate, etc.
Below is a complete command-line program that works for me. Tested in Delphi 5 and 2007. Why do you say IncMonth does not work for TDateTime?
program OneMonth;
{$APPTYPE CONSOLE}
uses
SysUtils,
Windows,
Messages;
procedure SetLocalSystemTime(settotime: TDateTime);
var
SystemTime : TSystemTime;
begin
DateTimeToSystemTime(settotime,SystemTime);
SetLocalTime(SystemTime);
//tell windows that the time changed
PostMessage(HWND_BROADCAST,WM_TIMECHANGE,0,0);
end;
begin
try
SetLocalSystemTime(IncMonth(Now,1));
except on E:Exception do
Writeln(E.Classname, ': ', E.Message);
end;
end.
IncMonth should work with a TDateTime:
function IncMonth ( const StartDate : TDateTime {; NumberOfMonths : Integer = 1} ) : TDateTime;
Keep in mind a TDate is really just a TDateTime that by convention your ignore the fraction on.
Based on your pseudocode:
procedure SetNewTime(aDateTime: TDateTime; aMonths: Integer);
var
lSystemTime: TSystemTime;
begin
DateTimeToSystemTime(aDateTime, lSystemTime);
Inc(lSystemTime.wMonth, aMonths);
setSystemTime(lSystemTime);
end;
setSystemTime uses UTC time, so you have to adjust for your time zone. The bias is the number of minutes your machine's timezone differs from UTC. This adjusts the date properly on my system:
procedure SetNewTime(aDateTime: TDateTime; aMonths: Integer);
var
lSystemTime: TSystemTime;
lTimeZone: TTimeZoneInformation;
begin
GetTimeZoneInformation(lTimeZone);
aDateTime := aDateTime + (lTimeZone.Bias / 1440);
DateTimeToSystemTime(aDateTime, lSystemTime);
Inc(lSystemTime.wMonth, aMonths);
setSystemTime(lSystemTime);
end;
There isn't enough information to provide a definitive answer to your question.
Consider what you would want to happen if the day of the current month doesn't exist in your future month. Say, January 31 + 1 month. (7 months of the year have 31 days and the rest have fewer.) You have the same problem if you increment the year and the starting date is February 29 on a leap year. So there can't be a universal IncMonth or IncYear function that will work consistantly on all dates.
For anyone interested, I heartily recommend Julian Bucknall's article on the complexities that are inherent in this type of calculation
on how to calculate the number of months and days between two dates.
The following is the only generic date increment functions possible that do not introduce anomolies into generic date math. But it only accomplishes this by shifting the responsibility back onto the programmer who presumably has the exact requirements of the specific application he/she is programming.
IncDay - Add a or subtract a number of days.
IncWeek - Add or subtract a number of weeks.
But if you must use the built in functions then at least be sure that they do what you want them to do. Have a look at the DateUtils and SysUtils units. Having the source code to these functions is one of the coolest aspects of Delphi. Having said that, here is the complete list of built in functions:
IncDay - Add a or subtract a number of days.
IncWeek - Add or subtract a number of weeks.
IncMonth - Add a or subtract a number of months.
IncYear - Add a or subtract a number of years.
As for the second part of your question, how to set the system date & time using a TDatetime, the following shamelessly stolen code from another post will do the job once you have a TDatetime that has the value you want:
procedure SetSystemDateTime(aDateTime: TDateTime);
var
lSystemTime: TSystemTime;
lTimeZone: TTimeZoneInformation;
begin
GetTimeZoneInformation(lTimeZone);
aDateTime := aDateTime + (lTimeZone.Bias / 1440);
DateTimeToSystemTime(aDateTime, lSystemTime);
setSystemTime(lSystemTime);
end;

Resources