Right guys i need you help please!

Im building a weather station that is meant to be updated every 1 hour. To save on time we are setiing the timer tick event to occur every 5 sec which is equal to 1 hour. Now heres the problem!

Every thing is working fine all the data is changing every 5 seconds but ive used the date time method to display the current day,month, date and the time but i want to add 1 hour to the time every time the timmer event occurs in my case every 5 secs. eg when the application beging it picks up the system time 13.45 then 5 seconds later all the data updates including the time to 14.50 and 5 seconds later 15.55 etc etc heres my code so far but in only happens on the first instance of the tick event

            string curDate = System.DateTime.Today.ToLongDateString();
            string curTime = System.DateTime.Now.AddHours(1).ToLongTimeString();
            string CurDay = System.DateTime.Today.DayOfWeek.ToString();
            string CurMonth = System.DateTime.Now.ToString("MMMM");

            labelDate.Text = curDate.ToString();
            labelTime.Text = curTime.ToString();
            labelDay.Text = CurDay.ToString();

tHANKS

Recommended Answers

All 5 Replies

You can't add 1 to the time and expect to get the result that you added 2. Make an instance variable, increment it each time your timer ticks and add it as to the hour.

If these variable assignments are in a local sub, they're getting re-initialized each time you run the sub. Try making them global, move them to the top of your main class before any subs or functions.

To save on time we are setiing the timer tick event to occur every 5 sec which is equal to 1 hour.

Is this a simulation or something?

only happens on the first instance of the tick event

You're using DateTime.Now as the starting point for the hour in all cases, which means the time will never be more than one hour beyond the current hour. You should be using the last stored time and extract the hour for update. Maybe something like this:

DateTime lastTime = DateTime.Parse(labelTime.Text);

labelTime.Text = new TimeSpan(
    lastTime.Hour + 1, 
    lastTime.Minute, 
    lastTime.Second).ToString();

Sorted with using a variable called count declared globaly and using the following code

string curDate = System.DateTime.Today.ToLongDateString();
string curTime = System.DateTime.Now.AddHours(count).ToLongTimeString();
string CurDay = System.DateTime.Today.DayOfWeek.ToString();
string CurMonth = System.DateTime.Now.ToString("MMMM");
labelDate.Text = curDate.ToString();
labelTime.Text = curTime.ToString();
labelDay.Text = CurDay.ToString();
count++

My problem now is incrementing the day and date when i get passed 23:59pm

Any ideas

It does that for you automatically

using System;

namespace timus {
    public class Prob1484 {
        public static void Main() {
            DateTime dt = DateTime.Now;
            for (int i = 1; i <= 10; i++) {
                DateTime ndt = dt.AddHours(i * 24);
                Console.WriteLine("{0}", ndt.ToLongDateString());
            }

            Console.ReadLine();
        }
    }
}

Results in

Tuesday, November 27, 2012
Wednesday, November 28, 2012
Thursday, November 29, 2012
Friday, November 30, 2012
Saturday, December 01, 2012
Sunday, December 02, 2012
Monday, December 03, 2012
Tuesday, December 04, 2012
Wednesday, December 05, 2012
Thursday, December 06, 2012
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.