1.11M Members

adding days to date..

 
0
 

hi, how do you add days automatically to current date? lets say i created an account in my program, and get its current date, augest 3 2011 for example, how would i add days automatically every after day, still keeps the date when i created my account. my goal is after 7 days, i want to remind my user to update his profile. i have this code add days.

DateTime tomorrow = DateTime.Today.AddDays( 7 );//
formattedDate = tomorrow.ToString("dd / MM / yy");
 
0
 

You can specify some date, like today (but you have to be aware that Today property does not set any day time, it is actually, but it sets to 00:00:00 "hh:mm:ss"), so then you can add 7 days to this date, and check if this date is a new date:

DateTime date1 = new DateTime(2011,8,3);
if(date1.AddDays(7) == new DateTime(2011, 8 10))
{
    //notify the user that 7 days has passed!
}
 
0
 

Could you use a DateTime compare and set the reminder if the result of the comparison is 7?

Will you use this in a way which will run this check every time a user logs in so that if it has been 7 days when the user logs in then the reminder is set ?

 
0
 

my users need to update every week(after 7 days). so if i created an account, "august 3, 2011, i need to update my accnt on aug 10. then next update will be on aug 17.

-- i will be using this every time the users logs in. it will check if 7 days have passed.. after update check again if 7 days have passed..

thanks.

 
0
 

This might be what your looking for rather than my other suggestion. The compare just tells you if one date is earlier, the same or before the other.

 
0
 

hi... i cant compare dates here..

when i create an account the date today is saved in the database. lets say i created account today, 8/10/2011 my next update will be on 8/17/2011. my program should check if 7 days have passed.

my code cant tell if 7 days have passed.

if ( DateTime.Now.Date.  >   Convert.ToDateTime(GlobalClass1.globalweekdt))
            {
                MessageBox.Show("please update");
            }

datetime.now.date =8 10 2011 Convert.ToDateTime(GlobalClass1.globalweekdt)=8 17 2010
this is not >

globalweekdt is the date from the database when i created an accnt it store the date he made an accnt.

 
0
 

See if this works if not i'm stuck too sorry

DateTime globalDT = Convert.ToDateTim(GlobalClass.globalweekdt);

int DTCompare = DateTime.Compare(DateTime.Now, globalDT);

if (DTCompare >= 7)            
{                
   MessageBox.Show("please update");            
}
 
0
 

messagebox still shows. still cant identify if 7 days have passed.. thanks anyways. (stuck)

 
0
 

Why do you need to convert GlobalClass.globalweekdt?
Have you checked what value the convertion returns.
e.g. MessageBox.Show("globalweekdt = {0}",Convert.ToDateTim(GlobalClass.globalweekdt)); If GlobalClass.globalweekdt is a string then it might be a localization issue due to different datetime formats.

 
0
 

I would use TimeSpan:

static void Main(string[] args)
      {
         /********************************************************
          * Uses a date 7 days before today 
          * compared to NOW, which will result in "Time to update"
          *******************************************************/
         DateTime dtThen = DateTime.Today - TimeSpan.FromDays(7);
         DateTime dtNow = DateTime.Now;

         TimeSpan tsDiff = (dtNow.Subtract(dtThen));

         if (tsDiff.Days >= 7)
         {
            Console.WriteLine("Time to update!");
         }

         /* **************************************************
          * Example 2
          * Uses a date 7 days before today
          * Uses another date 3 days before today
          * Increments the second day until "Time to update"
          * *************************************************/
         DateTime dtFakeToday = DateTime.Today - TimeSpan.FromDays(3);
         Enumerable.Range(1, 10).ToList().ForEach(i =>
         {
            if (dtFakeToday.Subtract(dtThen).Days >= 7)
            {
               Console.WriteLine("Time to update!");
            }

            dtFakeToday = dtFakeToday.Add(TimeSpan.FromDays(i));
         });
      }
 
0
 

Hi use this one,

TimeSpan TimeTaken = ((DateTime.Now - Convert.ToDateTime ("8/1/2011")));
        string TotalDays=    TimeTaken.Days .ToString ();

Check this date and display message

Hope this will help..........

 
0
 

Hi use this

TimeSpan TimeTaken = ((DateTime.Now - Convert.ToDateTime ("8/1/2011")));
        string Totaldays=    TimeTaken.Days .ToString ();

you can calculate the day....

Hope this will help.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article