Created
May 6, 2020 09:38
-
-
Save erluxman/45e30e5208b39123053f2408624d641c to your computer and use it in GitHub Desktop.
Dart extension methods
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 now = DateTime.now(); | |
var nextYear = now.nextYear; | |
var lastYear = now.previousYear(); | |
now.printYYYYMMdd("-"); | |
nextYear.printYYYYMMdd("/"); | |
lastYear.printYYYYMMdd("."); | |
} | |
extension DateExtensions on DateTime{ | |
printYYYYMMdd(String seperator) { | |
var dateString = "${this.year}$seperator${getTwoDigit(this.month)}$seperator${getTwoDigit(this.day)}"; | |
print(dateString); | |
} | |
String getTwoDigit(int number){ | |
return (number < 10)? "0$number" :number.toString(); | |
} | |
DateTime get nextYear => this.add(Duration(days:365)); | |
DateTime previousYear() => this.subtract(Duration(days:365)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment