Solved

I need a function to give next month

Posted on 2007-11-28
5
1,076 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 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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
Delphi TcxGrid group footer summary 3 389
Strange behavior when a form is closed 6 74
LAN or WAN ? 11 118
how can i delete one control based on its tagstring ? 6 56
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

738 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