question

VasanthakumarM-3635 avatar image
0 Votes"
VasanthakumarM-3635 asked VasanthakumarM-3635 commented

How to use Timer in Xamarin Forms

Hi Techie,
I have a requirement to add the timer in the App, this timer will take the current time, Whenever I open the app it will display the difference of current time and opened the app today's time . How I can achieve this .




dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered VasanthakumarM-3635 commented

Hello,​

Welcome to our Microsoft Q&A platform!

Please try to this code: MyLabel.Text = DateTime.Now.TimeOfDay.ToString();

88128-image.png

If you want to remove end of date, you can use

string str=DateTime.Now.TimeOfDay.ToString();

          var s=  str.Substring(0,str.Length-8);



88088-image.png

=====================Update=======================

Do you want to achieve it like this screenshot?(The displayed time is 11 hours later than the current one)

88196-image.png

If so, you can achieve it like this code.

string str = DateTime.Now.AddHours(-11).TimeOfDay.ToString();
            var s = str.Substring(0, str.Length - 8);
            mylabel.Text = s;


Best Regards,

Leon Lu



If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.




image.png (14.3 KiB)
image.png (86.7 KiB)
image.png (6.1 KiB)
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@LeonLu-MSFT My question is to differentiate (Subtract) the Current time - Tomorrow 9:00 am IST.

If I opened the app at 10:00 am. The timer will display 23:00:00,
If I opened the app at 11:00 am. The timer will display 22:00:00 ,
If I opened the app at 9:00 am. The timer will display 24:00:00 like this,

0 Votes 0 ·

Please see my updated answer.

0 Votes 0 ·

@VasanthakumarM-3635 Do you try following code, is that you needs?

string str = DateTime.Now.AddHours(-11).TimeOfDay.ToString();
            var s = str.Substring(0, str.Length - 8);
            mylabel.Text = s;
0 Votes 0 ·

@LeonLu-MSFT Nope. My need is different.

Clear example :

Now IST time is 20:22 pm . My targeted time is tomorrow at 9:00 am IST.

when u differentiate it: 12 hours 30 minutes 15 Seconds.

So this differentiated time will be displayed in the Label.

How to achieve that? Note : seconds will come like count down.







0 Votes 0 ·
LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered VasanthakumarM-3635 commented

@VasanthakumarM-3635 Do you want to achieve the result like this?

88458-ssss444.gif

My time is 15.50 now, so this differentiated time is 17 hours.

You can change the endtime by yourself. Then it will cacluate the countdown time automatically.

public partial class MainPage : ContentPage
    {

        DateTime endTime = new DateTime(2021, 4, 17, 9, 0, 0);
        public void StartCountDownTimer()
        {
            timer = new System.Timers.Timer();
            timer.Interval = 1000;
            timer.Elapsed += t_Tick;
            TimeSpan ts = endTime - DateTime.Now;
            cTimer = ts.ToString("d' Days 'h' Hours 'm' Minutes 's' Seconds'");
            timer.Start();
        }
        string cTimer;
        System.Timers.Timer timer;
        void t_Tick(object sender, EventArgs e)
        {
            TimeSpan ts = endTime - DateTime.Now;

           
            cTimer = ts.ToString("h':'m':'s' '");

            MainThread.BeginInvokeOnMainThread(() =>
            {
                // Code to run on the main thread
                mylabel.Text = cTimer;
            });

            if ((ts.TotalMilliseconds < 0) || (ts.TotalMilliseconds < 1000))
            {
                timer.Stop();
            }
        }

        public MainPage()
        {
            InitializeComponent();
            StartCountDownTimer();

         
        }
    }
}


My layout like following xaml.

<StackLayout>

        <Label x:Name="mylabel"></Label>
    </StackLayout>




ssss444.gif (6.6 KiB)
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@LeonLu-MSFT. Exactly!!

but every time I need to change the time and date of it. its impossible todo.

How can I do it dynamically? If it reaches the morning 9: 00 am automatically calculate next day 24 hours.

And Timer will display like this: 01:05:55 How can I zero before the count.

0 Votes 0 ·

but every time I need to change the time and date of it. its impossible todo.

How can I do it dynamically? If it reaches the morning 9: 00 am automatically calculate next day 24 hours.

Please replace endTime with following code.

DateTime endTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day+1, 9, 0, 0);

And Timer will display like this: 01:05:55 How can I zero before the count.

If you have other issue, please open a new thread, keep one thread for one question, it will help others have similar thread, find the solution quickly. And When you open a new thread, please write down the question you want to ask clearly and provide enough information so that the problem can be solved quickly.

0 Votes 0 ·

@LeonLu-MSFT It's showing an Error After long back I didn't change any code till now.


DateTime endTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day+1, 9, 0, 0);

Error name is 'Year, Month, and Day parameters describe an un-representable DateTime.'

0 Votes 0 ·
Show more comments