Created
May 18, 2018 05:01
-
-
Save debuggerx01/905b38caf23fce15026bad9239ba93aa to your computer and use it in GitHub Desktop.
DateFormat for 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
void main() { | |
var date = new DateTime.now(); | |
print(formatDate(date)); | |
} | |
String formatDate(DateTime date){ | |
return '${date.year}-' | |
'${fixInt2Str(date.month)}-' | |
'${fixInt2Str(date.day)} ' | |
'${fixInt2Str(date.hour)}:' | |
'${fixInt2Str(date.minute)}:' | |
'${fixInt2Str(date.second)}.' | |
'${fixInt2Str(date.millisecond,fix: 3)}'; | |
} | |
String fixInt2Str(int n,{fix = 2}){ | |
String res = n.toString(); | |
fix = fix - res.length; | |
for (;fix >0;fix--){ | |
res = '0$res'; | |
} | |
return res; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment