Skip to content

Instantly share code, notes, and snippets.

@graphicbeacon
Last active May 13, 2019 08:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save graphicbeacon/0d0d8cb8452af0f30cdf98d506053f7c to your computer and use it in GitHub Desktop.
Save graphicbeacon/0d0d8cb8452af0f30cdf98d506053f7c to your computer and use it in GitHub Desktop.
Create a countdown timer in Dart
<div>
<strong id="days"></strong>
<strong id="hours"></strong>
<strong id="minutes"></strong>
<strong id="seconds"></strong>
</div>
void main() {
var christmas2019 = DateTime(2019, 12, 25);
Timer.periodic(Duration(seconds: 1), (_) {
var now = DateTime.now();
var diff = christmas2019.difference(now);
var inMilli = diff.inMilliseconds;
var inSeconds = diff.inSeconds;
var seconds = (inSeconds % 60).floor();
var minutes = ((inSeconds / 60) % 60).floor();
var hours = ((inMilli / (1000 * 60 * 60)) % 24).floor();
var days = (inMilli / (1000 * 60 * 60 * 24)).floor();
querySelector('#days').innerHtml = '${days}d';
querySelector('#hours').innerHtml = '${hours}h';
querySelector('#minutes').innerHtml = '${minutes}m';
querySelector('#seconds').innerHtml = '${seconds}s';
});
}
@graphicbeacon
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment