473,396 Members | 1,706 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,396 software developers and data experts.

How to get the last date of the month?

Hi, I have a datetime variable. How can I get the date for the end of the
month according to that varilable's month and year?

Thanks,
Alpha
Nov 17 '05 #1
4 31218
DateTime dt = DateTime.Now;

DateTime firstDayOfThisMonth = dt.Subtract(TimeSpan.FromDays(dt.Day - 1));

Console.WriteLine("First Day of This Month:" + firstDayOfThisMonth);

DateTime firstDayOfNextMonth = firstDayOfThisMonth.AddMonths(1);

Console.WriteLine("First Day of next month" +
firstDayOfNextMonth.ToString());

DateTime lastDayOfThisMonth =
firstDayOfNextMonth.Subtract(TimeSpan.FromDays(1)) ;

Console.WriteLine("Last day of this month:"+lastDayOfThisMonth.ToString());
--
William Stacey [MVP]

"Alpha" <Al***@discussions.microsoft.com> wrote in message
news:6A**********************************@microsof t.com...
Hi, I have a datetime variable. How can I get the date for the end of the
month according to that varilable's month and year?

Thanks,
Alpha

Nov 17 '05 #2
"Alpha" <Al***@discussions.microsoft.com> wrote in message
news:6A**********************************@microsof t.com...
Hi, I have a datetime variable. How can I get the date for the end of the
month according to that varilable's month and year?

private DateTime LastDayOfMonth(DateTime pdtmDate)
{
DateTime dtmDate = new DateTime(pdtmDate.Year, pdtmDate.Month, 1);
return dtmDate.AddMonths(1).AddDays(-1);
}
Nov 17 '05 #3
Alpha wrote:
Hi, I have a datetime variable. How can I get the date for the end of the
month according to that varilable's month and year?

Thanks,
Alpha

DateTime d = new DateTime(Year, Month, DateTime.DaysInMonth(Year, Month));
Nov 17 '05 #4
Thanks for everyone's help and it's working.
Alpha

"Mark Rae" wrote:
"Alpha" <Al***@discussions.microsoft.com> wrote in message
news:6A**********************************@microsof t.com...
Hi, I have a datetime variable. How can I get the date for the end of the
month according to that varilable's month and year?

private DateTime LastDayOfMonth(DateTime pdtmDate)
{
DateTime dtmDate = new DateTime(pdtmDate.Year, pdtmDate.Month, 1);
return dtmDate.AddMonths(1).AddDays(-1);
}

Nov 17 '05 #5

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

Similar topics

13
by: SimonC | last post by:
I would like to return data from the last 2 weeks of each given month in Javascript, but in 2 formats. So, the penultimate week (Monday to Sunday) and the last week (Monday to ??) I'm not...
3
by: Gol Yass | last post by:
Dear Newsgroup, Is there a function in Access XP to take a date or month , and return the last day in that month? Ex: the last day in september is 30 or the last day in May is 31 . Thank...
3
by: Melissa | last post by:
I have this table: TblProjectYear ProjectYearID ProjectYearStartDate ProjectYearEndDate The Project Year will always span across December 31; for example 9/1/04 to 6/30/05. How do I build a...
7
by: MLH | last post by:
Public Function GetLastDayOfMonth(ByVal dtDay As Date) As Date '************************************************************************** ' Accepts a date. Determines month & year of the date....
5
by: hunt4grouse | last post by:
I'm looking for C code that will give me the last day of the month. Input - any day that I pass in as a variable.... input = 20050201 Output - calculate and format with slashes.... output =...
4
by: laredotornado | last post by:
Hi, Using PHP 4, if I have a date, what is a function I could use to give me a date that represents the first day of that month? For example, if my date were "3/19/2006 8:00", I would want my...
1
by: Svetac | last post by:
Hi, I use a script that shows when the page was last modified. It works fine for just one html file. The thing I would like to do is that the script shows me of last modified file in root...
3
by: ats | last post by:
Does anybody have any sample code for calculating the date for teh last Friday in each month. TIA -- ats@jbex When an old lady got hit by a truck I saw the wicked gleam in your eyes
11
by: Harlin Seritt | last post by:
Is there a module that can pull str values for say the last 3 months? Something like: print lastMonths(3) Thanks
0
by: marlberg | last post by:
Platform: Windows2000, WindowsXP, Windows Vista, etc Language: C#, ASP.NET Pre-compiled Libraries: Enterprise Library 3.0 full I have a requirement to implement in and display in C# and...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.