Solved

I need a function to give next month

Posted on 2007-11-28
5
1,054 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
thanks very much for the clue
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now