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 .
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 .
Hello,
Welcome to our Microsoft Q&A platform!
Please try to this code: MyLabel.Text = DateTime.Now.TimeOfDay.ToString();

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);

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

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.
@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,
@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;
@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.
@VasanthakumarM-3635 Do you want to achieve the result like this?

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>
@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.
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.
@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.'
7 people are following this question.