Last active
October 9, 2021 11:26
-
-
Save code-simple/b0c38822400ec328d396cba8e5f966bc to your computer and use it in GitHub Desktop.
To format your Time There is no need to Import whole Intl library, Just use this small Script, It has Months names, And add 0 if DateTime.now() returns single Digit
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
// Test Code | |
// https://dartpad.dev/?id=b0c38822400ec328d396cba8e5f966bc | |
void main() { | |
mytime() { | |
var now = DateTime.now(); | |
var monthList = [ | |
// Added 1st month Null because of Range Error | |
'Null', | |
'Jan', | |
'Feb', | |
'Mar', | |
'Apr', | |
'May', | |
'Jun', | |
'Jul', | |
'Aug', | |
'Sep', | |
'Oct', | |
'Nov', | |
'Dec' | |
]; | |
var c = [now.year,now.month,now.day,now.hour,now.minute,now.second]; | |
var d =[]; | |
// To add zero to single Digit | |
for (int i = 0; i < c.length; i++) { | |
if (c[i] < 10) { | |
d.add('0'+c[i].toString()); | |
}else{ | |
d.add(c[i]); | |
} | |
} | |
// Return format as you wish accordingly c[] values | |
// For months if you want return in digits then no need to use monthList | |
return (''' | |
${d[3]}:${d[4]}:${d[5]} ${d[2]}-${monthList[now.month]}-${d[0]} | |
'''); | |
} | |
print(mytime()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment