?
Solved

I need a function to give next month

Posted on 2007-11-28
5
Medium Priority
?
1,097 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 1500 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

750 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