Solved

I need a function to give next month

Posted on 2007-11-28
5
1,072 Views
Last Modified: 2012-08-14
Hello guys,

I will have a fix day for a date, I need a function that I will pass the fix day and it will return the next month with my day, if this day will be weekend, it must to add 1 or 2 days to return me monday.

Example:

I am 11/28/2007  my fix day is 5

NextMonth(5) = 12/05/2007
if this date will be weekend, for example, saturday, then
it will return me  12/07/2007

thanks
0
Comment
Question by:hidrau
  • 3
  • 2
5 Comments
 
LVL 13

Expert Comment

by:rfwoolf
ID: 20365405
To increment 5 days ahead you can add DateUtils to the uses clause of your unit, and use the  
IncDay function, which uses this syntax:
IncDay(const AValue: TDateTime; const ANumberOfDays: Integer): TDateTime;

Example:
IncDay(Today, 5);

Next you can use the DayOfTheWeek function to return the number of the day of the week, where Monday to Friday is 1 to 5, Saturday is 6 and Sunday is 7.
Syntax:
function DayOfTheWeek(const AValue: TDateTime): Word;
Example:
DayOfTheWeek(Today) //is 3 for Wednesday.

===

Here's a full example:

var
i : TheDayOfTheWeek
Begin
  MyDate := IncDay(MyDate,5);
  i := DayOfTheWeek(MyDate);
  If i > 5 then
  MyDate := IncDay(MyDate, 8 - i);
  Showmessage(DateToStr(MyDate));
end;

Open in new window

0
 
LVL 13

Expert Comment

by:rfwoolf
ID: 20365423
Minor corrections, here's the updated example:
...
uses
dateutils;
 
...
var
i : integer; //Holds the day of the week
MyDate : TDateTime;
begin
  //Incrememnt the date by 5 days
  MyDate := IncDay(MyDate,5);
  //Find out what day of the week it is
  i := DayOfTheWeek(MyDate);
  //If it's a Saturday or Sunday...
  If i > 5 then
    MyDate := IncDay(MyDate, 8 - i);
  //output
  Showmessage(DateToStr(MyDate));
end;

Open in new window

0
 
LVL 1

Author Comment

by:hidrau
ID: 20366493
I had to change the function because I am getting a wrong years and month,

  MyDate := IncMonth(Date);
  MS     := MonthOf(MyDate);
  MyDate := StrToDate(IntToStr(Dias) + '/' + IntToStr(MS) + '/' + IntToStr(YearOf(Date)));
  //Find out what day of the week it is

  i := DayOfTheWeek(MyDate);

  //If it's a Saturday or Sunday...
  If i > 5 then
    MyDate := IncDay(MyDate, 8 - i);
  //output
  Result := MyDate;
0
 
LVL 13

Accepted Solution

by:
rfwoolf earned 500 total points
ID: 20366610
Problem!

Whenever converting from String to Date you must be careful.
If your program runs on a machine where the locale settings are different, and therefore the date format is different, your program will raise an exception and the procedure will fail.

Example:
You are in America, and you say:
StrToDate('11/29/2007') . You compile it and it runs because your system expects date format mm/dd/yyyy
Your client in Australia tries this
StrToDate('11/29/2007'). He gets an exception because his system is expecting date format dd/mm/yyyy.

For this purpose you may need to set the date format.

An alternative is to use the EncodeDate function, which has a universal format.
i.e.
MyDate := EncodeDate(YearOf(Date), MS, Dias);

Here is the syntax for EncodeDate function:
[Delphi] function EncodeDate(Year: Word; Month: Word; Day: Word): TDateTime;
0
 
LVL 1

Author Comment

by:hidrau
ID: 20366697
thanks very much for the clue
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Newbie Thread Programming 1 161
error 1.1 400 Bad request idhttp delphi 18 150
How to build JSON File in Delphi 6 3 46
Breakpoint doesn't stop in my variable 3 30
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question