Reputation: 1347
I'm attempting to only display data added to my database 24 hours ago or less. However, for some reason, the code I've written isn't working, and both entries in my database, one from 1 hour ago, one from 2 days ago, show up. Is there something wrong with my equation? Thanks!
public void UpdateValues()
{
double TotalCost = 0;
double TotalEarned = 0;
double TotalProfit = 0;
double TotalHST = 0;
for (int i = 0; i <= Program.TransactionList.Count - 1; i++)
{
DateTime Today = DateTime.Now;
DateTime Jan2013 = DateTime.Parse("01-01-2013"); //Hours since Jan12013
int TodayHoursSince2013 = Convert.ToInt32(Math.Round(Today.Subtract(Jan2013).TotalHours)); //7
int ItemHoursSince2013 = Program.TransactionList[i].HoursSince2013; //Equals 7176, and 7130
if (ItemHoursSince2013 - TodayHoursSince2013 <= 24)
{
TotalCost += Program.TransactionList[i].TotalCost;
TotalEarned += Program.TransactionList[i].TotalEarned;
TotalProfit += Program.TransactionList[i].TotalEarned - Program.TransactionList[i].TotalCost;
TotalHST += Program.TransactionList[i].TotalHST;
}
}
label6.Text = "$" + String.Format("{0:0.00}", TotalCost);
label7.Text = "$" + String.Format("{0:0.00}", TotalEarned);
label8.Text = "$" + String.Format("{0:0.00}", TotalProfit);
label10.Text = "$" + String.Format("{0:0.00}", TotalHST);
}
Upvotes: 1
Views: 77
Reputation: 4542
Place it inside your for loop(the datetime variable now place it outside):
DateTime now = DateTime.Now;
DateTime TransactionListDate = Program.TransactionList[i].HoursSince2013;
if (TransactionListDate > now.AddHours(-24) && TransactionListDate <= now)
{
//it falls between now and last 24 hours....
}
I think this is what you need.I assume Program.TransactionList[i].HoursSince2013 is the datetime.
Upvotes: 2