You can use DateTime
's DayOfYear
property.
int dayOfYear = DateTime.Now.DayOfYear;
Pregunta
I want to know the number of days from january 1st to today.
If today is January 10th, then numOfDays=10
, if today is February 1st then numOfDays=32
.
How can I get the total no of days? Thank you
Solución
You can use DateTime
's DayOfYear
property.
int dayOfYear = DateTime.Now.DayOfYear;
Otros consejos
DateTime.DayOfYear
is exactly what you want.
To find out the day of year for today:
var days = DateTime.Today.DayOfYear;
This should give you what you are looking for:
int currDayOfYear = DateTime.Now.DayOfYear;
This shows how to do it. Step by step, use DateTime.Now to get a DateTime object representing the current date/time. Then use the DateTime.DayOfYear property.
http://msdn.microsoft.com/en-us/library/system.datetime.dayofyear.aspx
public int DayOfYear { get; }
Property Value Type: System.Int32 The day of the year, expressed as a value between 1 and 366.
Datetime.Now explained: http://msdn.microsoft.com/en-us/library/system.datetime.now.aspx