Last active
May 13, 2019 08:24
-
-
Save graphicbeacon/0d0d8cb8452af0f30cdf98d506053f7c to your computer and use it in GitHub Desktop.
Create a countdown timer in Dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div> | |
<strong id="days"></strong> | |
<strong id="hours"></strong> | |
<strong id="minutes"></strong> | |
<strong id="seconds"></strong> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See this in action: https://dartpad.dartlang.org/0d0d8cb8452af0f30cdf98d506053f7c