[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

I need a function to give next month

Posted on 2007-11-28
5
Medium Priority
?
1,087 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

649 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