Eps,
It should be noted that this code doesn't compile. What you want is:
DateTime FirstDayInMonth =
DateTime.Now.Subtract(TimeSpan.FromDays(DateTime.N ow.Day - 1));
Now, with that, you are going to get the first day of the month, but the
time will be whatever time it is when you run the code. If you want the
beginning of the day, then what you really want to do is this:
DateTime now = DateTime.Now;
DateTime FirstDayInMonth = new DateTime(now.Year, now.Month, 1);
This will give you midnight on the first day of the month.
It also eliminates a subtle bug that existed when you called the static
Now property twice on the DateTime structure. If you ran this around
midnight, you had the chance that the first call to Now would return the day
before midnight, and the second call to Now would result in the day after
midnight, which would give you an incorrect result. You would want to call
Now ^once^, storing the value, and then using that.
--
- Nicholas Paldino [.NET/C# MVP]
-
mv*@spam.guard.caspershouse.com
"eps" <ep*@mailinator.comwrote in message
news:uD**************@TK2MSFTNGP06.phx.gbl...
Hi there,
The following code gets the first day in the current month, I don't
understand how it works though, could someone try to explain it to me ?.
DateTime FirstDayInMonth =
DateTime.Now.Subtract(TimeSpan.FromDays(Month.Day - 1));
Any help appreciated.
--
Eps