minimum date of the year
I have one text box and i have to show minim开发者_JAVA百科um date of the current year in that texbox
that means: suppose consider if i select current_time (09/11/2009) then(01/01/2009) should be shown in text box
awaiting ur response
You just want to construct a new DateTime
with the same year, and other parameters set to one (for first day and month).
var currentDateTime = DateTime.Now; // or whatever else
var firstDayOfYear = new DateTime(currentDateTime.Year, 1, 1);
Edit: As Noldorin points out, the simplest and most terse solution is something like this:
var firstDayOfYear = new DateTime(DateTime.Today.Year, 1, 1);
My old answer:
This should do it.
var now = DateTime.Now
var firstDayOfYear = now.Subtract(TimeSpan.FromDays(now.DayOfYear - 1)).Date
Edit: Actually this would be a bit cleaner:
var today = DateTime.Today
var firstDayOfYear = today.Subtract(TimeSpan.FromDays(today.DayOfYear - 1))
I have had to solve this same problem in the past. In the end, I decided to create an Extension Methods class for the DateTime class to encapsulate and test the albeit simple functionality.
public static class DateTimeExtensions
{
public static DateTime GetFirstDayOfYear(this DateTime date)
{
return new DateTime(date.Year, 1, 1);
}
}
Now all I would have to do to get the date required is
DateTime selectedDate = DateTime.Today;
DateTime firstDayOfYear = selectedDate.GetFirstDayOfYear();
Assuming you can get a DateTime object from the textbox (e.g. DateTime.Parse(...)) you can do this:
DateTime inputDate = DateTime.Parse(textBox.Text);
DateTime outputDate = new DateTime(inputDate.Year, 1, 1);
精彩评论