How to use Timer in Xamarin Forms

Vasanthakumar M 251 Reputation points
2021-04-15T07:23:11.187+00:00

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 .

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,355 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 75,846 Reputation points Microsoft Vendor
    2021-04-15T08:29:51.847+00:00

    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.


1 additional answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 75,846 Reputation points Microsoft Vendor
    2021-04-16T07:55:57.583+00:00

    @Vasanthakumar M 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>  
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.