473,385 Members | 2,269 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

how to get the end date of a month

Bob
I passed in a DateTime object, and want to get its end date of the month. Is
there a easy way to do it?

Thanks.
Jul 21 '05 #1
7 1996
How about something like

(new DateTime(n,1,04)).AddMonth(1).AddDay(-1);

Have not compiled this, but you get the idea.

"Bob" <bo******@yahoo.com> wrote in message
news:#A**************@TK2MSFTNGP12.phx.gbl...
I passed in a DateTime object, and want to get its end date of the month. Is there a easy way to do it?

Thanks.

Jul 21 '05 #2
I posted this a few weeks ago the last time it was asked :-)

Public Function EndOfMonth(ByVal d As Date, ByVal m As Integer) As Date
Return d.AddMonths(m + 1).AddDays(-d.Day)
End Function

If you send it a date and 0 for the m (months) parameter it returns the end
of the month for the date. Plus you can send it a 1, 2, 12 or whatever for
months in the future. I assume it will work with months in the past but I
can't recall if I tested that.

Tom

"Bob" <bo******@yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
I passed in a DateTime object, and want to get its end date of the month. Is there a easy way to do it?

Thanks.

Jul 21 '05 #3
Cor
Hi Bob,

I saved this very well, it is from NAK who was active here some time ago and
we had fun when he was making it, because somebody was asking how to get the
last day of the week, but than it became the month. It is maybe not your
question, but I have saved it so long that I send it and maybe you can use
it.

:-))

The following function will tell you if it is currently the last
"monday,tuesday,wednesday.." of the month. For example. if you pass it
Saturday today (30 August 2003) it will return True.
\\\
'Made by Nick Paterman
Public Function isLastDayOfMonth(ByVal iDay As DayOfWeek) As Boolean
If (Date.Now.DayOfWeek = iDay) Then
If (Date.Now.Day < Date.DaysInMonth(Now.Year, Now.Month)) Then
Dim pIntDaysLeft As Integer = Date.DaysInMonth(Now.Year,
Now.Month) - Date.Now.Day
Return (pIntDaysLeft < 7)
Else
'IS THE LAST DAY OF THE MONTH!
Return (True)
End If
Else
'THAT ISNT TODAYS DAY
Return (False)
End If
End Function
///

I hope this helps a little bit?

Cor
Jul 21 '05 #4
Bob
Thanks, Guys!!
"Bob" <bo******@yahoo.com> wrote in message
news:#A**************@TK2MSFTNGP12.phx.gbl...
I passed in a DateTime object, and want to get its end date of the month. Is there a easy way to do it?

Thanks.

Jul 21 '05 #5
Cor,

Interesting way to handle it... so if I want to know which day of the week
is the end of the month I call it in a loop from 1 to 7 and break when I get
a True?

I probably would have it return the LastDayOfMonth and let the client a) use
the value directly or b) run through their own local loop or case statement
without the need to keep asking the function if it guessed correctly yet.
But that's just me :-)

Tom
"Cor" <no*@non.com> wrote in message
news:Oa**************@TK2MSFTNGP09.phx.gbl...
Hi Bob,

I saved this very well, it is from NAK who was active here some time ago and we had fun when he was making it, because somebody was asking how to get the last day of the week, but than it became the month. It is maybe not your
question, but I have saved it so long that I send it and maybe you can use
it.

:-))

The following function will tell you if it is currently the last
"monday,tuesday,wednesday.." of the month. For example. if you pass it
Saturday today (30 August 2003) it will return True.
\\\
'Made by Nick Paterman
Public Function isLastDayOfMonth(ByVal iDay As DayOfWeek) As Boolean
If (Date.Now.DayOfWeek = iDay) Then
If (Date.Now.Day < Date.DaysInMonth(Now.Year, Now.Month)) Then
Dim pIntDaysLeft As Integer = Date.DaysInMonth(Now.Year,
Now.Month) - Date.Now.Day
Return (pIntDaysLeft < 7)
Else
'IS THE LAST DAY OF THE MONTH!
Return (True)
End If
Else
'THAT ISNT TODAYS DAY
Return (False)
End If
End Function
///

I hope this helps a little bit?

Cor

Jul 21 '05 #6
Cor
Hi Tom,

I was so happy I could send it, I saved it well just for an occassion that
it should happen. It was something Nick has been very busy with, maybe he
sees this and we see him again.

I was thinking if I would make something myself. Because the end date of the
month is the date of the first day of the next month minus 1. But I saw so
many answers already that I only did send this.

Cor

Interesting way to handle it... so if I want to know which day of the week
is the end of the month I call it in a loop from 1 to 7 and break when I get a True?

Jul 21 '05 #7
Bob <bo******@yahoo.com> wrote:
I passed in a DateTime object, and want to get its end date of the month. Is
there a easy way to do it?


Aside from the other ways people have suggested:

int lastDay = DateTime.DaysInMonth (myDate.Year, myDate.Month);
DateTime endOfMonth = myDate.AddDays(lastDay-myDate.Day);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: androtech | last post by:
Hello, I'm looking for a function that returns a date range for a specified week number of the year. I'm not able to find functions like this anywhere. Any pointers/help would be much...
4
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
7
by: Adrian | last post by:
I hit on this problem converting a VB.NET insurance application to C#. Age next birthday calculated from date of birth is often needed in insurance premium calculations. Originally done using...
12
by: Assimalyst | last post by:
Hi, I have a working script that converts a dd/mm/yyyy text box date entry to yyyy/mm/dd and compares it to the current date, giving an error through an asp.net custom validator, it is as...
2
by: Niyazi | last post by:
Hi everyone, I have a sql table that has 5 column as: cl1Month - cl1_3Month - cl3_6Month - cl6_12Month - clMoreThan12Month Now I have to date and I have to find the differences and check as...
3
by: Bob Sanderson | last post by:
I have a PHP web page which uses a HTML form. I would like to enter dates into the date fields using a JavaScript calendar, similar to the way phpMyAdmin does. Can anyone recommend a JavaScript...
10
by: Jes | last post by:
Dear all I have a date field on a HTML form where the user is asked to key in dd/mm/yyyy However, when that is written to MySql it is either not accepted or another value is tored in the...
10
by: ashore | last post by:
Guys, the line below just returned "Dec 07" as the date for one month back from today. Hardly life-threatening, but any thoughts? <?php print date("M `y", mktime(0, 0, 0, date("m")-1, date("d"),...
2
by: mshroom12 | last post by:
I am having trouble with the following project on hand. I use Eclipse to do my work in Java. This is what I'm supposed to complete. Date Validation In this exercise you will write a program...
3
by: janetopps | last post by:
I have a news website, with asp pages, which was on Access, and i upgraded to MySQL, i used Bullzip to transfer the data. It had about 1000 pages, which im now able to pull up on the public side. Im...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.