Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i wish to get the last date of the month in the format of MM/DD/YYYY Only,

unfortunately, the one i have doesn't not give me that.

What I have tried:

DateTime lastday = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.DaysInMonth(DateTime.Today.Year, DateTime.Today.Month));
Posted
Updated 1-Aug-16 22:16pm

1 solution

Dates in DateTime don't have a format, they only gain a format when you convert them to a string, so if you want the last day in MM/dd/yyyy then do

C#
string dateToShow = lastday.ToString("MM/dd/yyyy");
 
Share this answer
 
v2
Comments
Emmablakes 2-Aug-16 4:22am    
I tried this but it still came out like 8/31/2016 12:00AM.
i dont need the 12:00am part...i just want 8/31/2016.
Garth J Lancaster 2-Aug-16 4:46am    
there is no way the provided code

string dateToShow = lastday.ToString("MM/dd/yyyy");

prints anything to do with the time - it has format specifiers for Month, Day, Year, separated by '/' - no time components at all - I'd suggest you've made an error somewhere in your code - please check it using the solution supplied - if you're seeing the time when you inspect the lastday DateTime variable in debug mode, thats fine - but this code simply does not write those time values to the console
F-ES Sitecore 2-Aug-16 5:05am    
You'll need to explain how that is getting rendered to the screen as what you do to format the date depends on how it is being rendered.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900