tags:

views:

193

answers:

5

Today is 5.27.2010 - this means it is day 147 of this year.

How do I calculate that today is 147 based on the current date?

+9  A: 

There's a DateTime property named just that: DayOfYear

Console.WriteLine(DateTime.Now.DayOfYear);

Or for any date:

var d = new DateTime(2010, 5, 30);
Console.WriteLine(d.DayOfYear);
Ahmad Mageed
+3  A: 

Actually, it's pretty easy:

int dayOfYear = DateTime.Today.DayOfYear;
R. Bemrose
+3  A: 

C#'s DateTime class has a method called DayOfYear() that you could use.

Khnle
+3  A: 

Has anyone mentioned the DateTime.DayOfYear property? lol =P

Will Marcouiller
I hear that there's a propery called DayOfYear he could use :)
Simon
I'd like to upvote you, but SO doesn't have a "+1, Funny" mod. :-P
Craig Walker
Seems a little obvious...
John M
@Simon: Are you serious! ;) Hehehe...
Will Marcouiller
@Craig Walker: Thanks for this "+1 Funny" comment! =)
Will Marcouiller
@John M: I meant no harm, indeed! I just wanted to laugh a bit. Nothing personal! =) Have a nice day!
Will Marcouiller
Simon, where's this `DayOfYear` property defined?
Phong
+1  A: 
DateTime dt = new DateTime(2001, 12, 14);
            dynamic dayofyear = dt.DayOfYear;
            dynamic datofweek = dt.DayOfWeek;
4thpage
Why dynamic type?
Jace Rhea
means whenever you want you can convert it to anything else like string , int and datetime easily
4thpage
Its just as easy to convert it with a static type, and it enforces typing rules. There is no reason to use dynamic here.
Jace Rhea