Domanda

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

È stato utile?

Soluzione

You can use DateTime's DayOfYear property.

int dayOfYear = DateTime.Now.DayOfYear;

Altri suggerimenti

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top