how do I get data between two dates

I would like to get data between two years, so from 1st of Jan to 1st of Jan, This is what i have done:

public list<a>method(){
    DateTime d= new DateTime(DateTime.Now.Year, 01, 1);
    DateTime dd= new DateTime(DateTime..Year, 01, 1); <- instead 'now' what is the syntax for next year?
    SqlCommand command = new SqlCommand("SELECT TOP 100 ID FROM TableNameWHERE Time Between @Time AND @Time1 ORDER BY Time OFFSET 10 ROWS FETCH NEXT 100 ROWS ONLY", conn);
    command.Parameters.AddWithValue("@Time", d);
    command.Parameters.AddWithValue("@Time1", dd);
}
Jon Skeet
people
quotationmark

If you just want "the next year" it's simple:

// Note: more descriptive variable names than "d" and "dd"
int thisYear = DateTime.Now.Year; // Note this is in the local time zone...
DateTime thisYearStart = new DateTime(thisYear, 1, 1);
DateTime nextYearStart = new DateTime(thisYear + 1, 1, 1);

Or:

int thisYear = DateTime.Now.Year;
DateTime thisYearStart = new DateTime(thisYear, 1, 1);
DateTime nextYearStart = thisYearStart.AddYears(1);

Note the comment about time zones - use UtcNow if you want the "UTC year", in which case you probably want to specify a DateTimeKind of Utc as well.

EDIT: This is assuming you can use an inclusive start time and an exclusive end time. That's generally a nice way of working (because the next inclusive start time ends up being the current exclusive end time, and you don't need to worry about granularity). However, if you want the last tick of the current year for an inclusive upper bound, you can use:

int thisYear = DateTime.Now.Year;
DateTime thisYearStart = new DateTime(thisYear, 1, 1);
DateTime thisYearEnd = thisYearStart.AddYears(1).AddTicks(-1);

people

See more on this question at Stackoverflow