Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# Instantiating Class Instances

Dmitri Mikhalev
Dmitri Mikhalev
4,462 Points

How do I set a DateTime field to a specific date?

I believe that we only practiced ".Now" and ."Today" earlier, or I might just have forgotten about it. Anyways.

Say that I am instantiating a ReleaseDate class instance and I want to set the date to 4the June, 2002.. what should I put to the right side of the equals sign? :

album.ReleaseDate = (2002, 6, 4)????;

The field in question:

 public DateTime ReleaseDate { get; set; }
Dmitri Mikhalev
Dmitri Mikhalev
4,462 Points

Like this?

album.ReleaseDate = new DateTime(2002, 6, 4);

1 Answer

Steven Parker
Steven Parker
230,178 Points

Looks like you resolved your own issue, congratulations!   :+1:

Another way you could do it:

album.ReleaseDate = Convert.ToDateTime("06/04/2002");