Link to home
Start Free TrialLog in
Avatar of jcampanali
jcampanali

asked on

Display First Day of Month C#

Hello,

I'm trying to avoid using Left/Right properties to extract numbers that will built a date like 11/1/2010-basically I want to display the current month, current year with day 1. Here's what I have thus far...

tmpExDate = Strings.Left(Convert.ToString(Module1.ExDateVal), 2);  //ExDateVal is a DateTime variable
    tmpExDate = tmpExDate + "-1-";
    tmpExDate = tmpExDate + Strings.Right(Convert.ToString(Module1.ExDateVal), 4);
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Much simpler option:

Dim tmpExDate As New DateTime(Module1.ExDateVal.Year, Module1.ExDateVal.Month, 1)

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of shadow77
shadow77


DateTime dt = DateTime.Now;
    string s = string.Format("{0:D2}-1-{1:D4}", dt.Month, dt.Year);

Open in new window

DateTime now = DateTime.Now;
DateTime firstDayofMonth = new DateTime(now.Year, now.Month, 1);
string s = firstDayofMonth.ToShortDateString();
DateTime firstDayofMonth = DateTime.Today.AddDays(-(DateTime.Today.Day - 1));