Tip

Last day of a month redux (any location)

A recent tip showed how to calculate the last day of a month. It worked nicely, but unfortunately, only if your date format is dd/mm/yy (UK, I think).

This version works anywhere.

Basic idea: Advance the day into the next month, then go back to the first day of the next month -- and one day further.


start_date := @Today;
next_month := @Adjust(start_date; 0; 1; 0; 0; 0; 0);
last_of_month := @Adjust(next_month; 0; 0; -@Day(next_month); 0; 0; 0);

The -@Day(next_month) actually advances to the last day of the previous month, which is really our start month. Think about it...

start date: July 3, 1968
next month: August 3, 1968
Day of next month: 3
August 3 - 2 days is August 1, but August 3 - 3 days is July 31.

This works no matter what month, no matter what year, no matter what day of the month you start on. Since it doesn't use any text conversion, it doesn't rely on any assumptions as to date format.

This was first published in September 2001

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.