Click here to Skip to main content
16,011,988 members
Please Sign up or sign in to vote.
4.33/5 (2 votes)
See more:
Get past 2days and future 2days records using the offset property. If i get past 2day i can use my offset property something like this "string offset = -2", but if i want to use both past and future, how to calculate?

Thanks in advance.
Posted

C#
// required
using System.Linq;

// generate range: -2,-1,0,1,2
private IEnumerable<int> FourDays = Enumerable.Range(-2, 5);

// somewhere in a method

DateTime rightNow = DateTime.Now;

// test ...
foreach (var i in FourDays)
{
    // test ...
    Console.WriteLine(rightNow.AddDays(i));

    // call method to get Records
    // ...
}</int>
 
Share this answer
 
Try this

C#
int offset = 2 ;
          DateTime past = DateTime.Now.AddDays(-offset);
          DateTime future = DateTime.Now.AddDays(offset);
 
Share this answer
 
Hello, to manipulate a date using the functions of DateTime class.
http://msdn.microsoft.com/en-us/library/system.datetime.adddays(v=vs.110).aspx[^]

C#
DateTime now = Datetime.Now;
DateTime 2daysAgo = now.AddDays(-2);
DateTime 2daysLater = now.AddDays(2);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900