I have a datetime object, dt, that holds today’s date.
DateTime dt = DateTime.Today;
DateTime idt = new DateTime();
I need to print the date when the loop reaches last day of the current year, like this:
345 more days to 12/31/2012
If I do it this way
while (dt.AddDays(i).Year == dt.Year)
{
idt = dt.AddDays(i);
i++;
}
Console.WriteLine("{0} more days to {1}", i, idt.ToString("d"));
I am able to get it to print like I want: 345 more days to 1/1/2012
I want to avoid the repetition of dt.AddDays(i) in the above part. If I do it either this way:
while (idt.Year != dt.Year + 1)
{
idt = dt.AddDays(i);
i++;
}
Console.WriteLine("{0} more days to {1}", i, idt.ToString("d"));
or this way:
while (idt.Year < 2013)
{
idt = dt.AddDays(i);
i++;
}
Console.WriteLine("{0} more days to {1}", i, idt.ToString("d"));
I get the output as 346 more days to 1/1/2013.
I am definitely missing something painfully obvious, but I am unable to figure out. What would it be?
Why not just
?