Skip to content

Instantly share code, notes, and snippets.

@debuggerx01
Created May 18, 2018 05:01
Show Gist options
  • Save debuggerx01/905b38caf23fce15026bad9239ba93aa to your computer and use it in GitHub Desktop.
Save debuggerx01/905b38caf23fce15026bad9239ba93aa to your computer and use it in GitHub Desktop.
DateFormat for Dart
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