Reputation: 545
I created a windows 8 app that will have a countdown timer. I have the code for the timer and it works fine, but instead of counting down from 120 seconds I want it to be displayed as 2:00 minutes and countdown from there. But because I use a timer I am not sure how to use the date/time property (if I can even do that). Please help me figure this out.
Here is my timer code:
DispatcherTimer timer;
private int counter = 120;
public ArcadeMode()
{
InitializeComponent();
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += timer_Tick;
timer.Start();
}
async void timer_Tick(object sender, object e)
{
await Time.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Low, () => { Time.Text = counter.ToString(); });
counter--;
}
Upvotes: 3
Views: 468
Reputation: 1709
convert it to Datetime instance, and .ToString("the format you want")
Upvotes: 2
Reputation: 63065
Time.Text = TimeSpan.FromSeconds(counter).ToString();
this will give you output like 00:01:00
if input is 60
Upvotes: 6
Reputation: 66449
Maybe there are faster/terser ways, but this should at least display the timer the way you'd like (untested):
Time.Text = string.Format("{0}:{1}", (counter / 60), (counter % 60).ToString().PadLeft(2,'0'));
Upvotes: 3