Thanks:  0
Likes:  0
Dislikes:  0

1. ## Validate the Date

Hi;

I would Like to find that the whether the "Current Date" be lying with in the the Last week of Month or not.

How can I validate.

Thanks;
Sunil

2. ## Re: Validate the Date

Add 7 days to the current date and compare the month of the new date with the current month.

If that expression is equal to 1, the current date is in the last week of the month.

3. ## Re: Validate the Date

Nice logic Christian [img]/images/graemlins/smile.gif[/img]

Mark Smith.

4. ## Re: Validate the Date

[ QUOTE ]

[/ QUOTE ]

How does this checks whether current date is within last week of month?

5. ## Re: Validate the Date

For today's date :

which will become
DateDiff('m', 7/15/2009, 7/8/2009)
and the difference is 0, hence not the last week of the month.

For future date :

which will become
DateDiff('m', 8/3/2009, 7/27/2009)
and the difference is 1, hence it is the last week of the month.

Very nice logic...

6. ## Re: Validate the Date

[ QUOTE ]
Very nice logic...

[/ QUOTE ]

I don't think so

How about "8/26/2009"? its not in the last week of the month but the above logic returns 1

Thanks,
Pavan

8. ## Re: Validate the Date

<font class="small">Code:</font><hr /><pre>Today = Now
DaysInFirstWeek = 7 - cInt(WeekDay(Month(Today)&amp;"/01/"&amp;Year(Today))) + 1
If DaysInFirstWeek + 21 &lt; Day(Today) And DaysInFirstWeek + 28 &gt;= Day(Today) Then

Print "Current Day is in last week of month"
Else
Print "Current Day is not in last week of month"

End If</pre><hr />

9. ## Re: Validate the Date

[ QUOTE ]
How does this checks whether current date is within last week of month?

[/ QUOTE ]

Most of the times, when a program does not provide the results you expect, it is because you and the programmer have had a different understanding of its goals (or requirements). And, in fact, whether this oversimplified calculation gives the right answer depends on the definition you give to the "last week of the month".

We have 3 words to define : last, week, month. Let's examine them in the reverse order, as we would if we were to use an object oriented language.

The definition of a month depends on the calendar you use (see http://en.wikipedia.org/wiki/Month). For sake of simplicity, I will use the Gregorian calendar, which happens to be the one used by VBScript date functions.

In the Gregorian calendar, a week is defined a seven consecutive days (see http://en.wikipedia.org/wiki/Gregorian_calendar#Week). But weeks do not begin on the same day and are not numbered the same in all the countries that use the Gregorian calendar (see http://en.wikipedia.org/wiki/7_Day_Week#Week_number). To account for that fact, the VBScript date functions can use special values for the first day of the week and the first week of the year, namely vbUseSystemDayOfWeek and vbUseSystem. Once again, for sake of simplicity, I will use these special values in some computations.

Finally, we have to define what is the "last" week of a month. From this discussion (http://stackoverflow.com/questions/6...eek-of-a-month) for instance, it appears that there is no unique definition of such a last week. So, the attached program allows you to define the "last week of a month" as
- the one that holds the last day of the month
- the one that holds the last first day of week, as defined by the system locale
- the one that holds the last sunday, monday, tuesday, wednesday, thursday, friday or saturday.

Usually, in our field, we use the last friday (think of your backup procedures). ISO 8601 somehow implies that we use thursday.

I attach the proposed solution(s) as LastWeekOfMonthVBS.txt (rename to .vbs and run it). If you run in a command window with cscript and redirect its output, you'll have something similar to the resultsMine.txt attached file that you can open in Excel to check the results for year 2009. I would be happy to know if it gives consistent results in other countries.

- I added my first answer as a separate method, as it does not rely on the assumption that a week begins on a given day. It should not be a surprise, but you'll see that is gives different results from the new computation, whatever option you take for it.

- I think thet the above code, by TReaddy, does not give consistent results (check what happens in February 2009 for instance).

Final note : it took me more time to bring up this proposed solution than it would have taken to just build a list of "last week" numbers for the coming 3 or 5 years (probably the maximum life time of any application - and its test harness - nowadays). If I were the original poster, I would just ask the stakeholders for their definition of the "last week of month" and use a constant array in the test programs.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
Search Engine Optimisation provided by DragonByte SEO v2.0.36 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.