Created
April 8, 2024 22:41
-
-
Save ashraf267/e346601834a91f95a67664a073746292 to your computer and use it in GitHub Desktop.
custom formatted date string
This file contains hidden or 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() { | |
String dt = formatDate(); | |
print(dt); | |
} | |
// format my date | |
// return a custom date formatted string | |
String formatDate() { | |
// dt obj | |
DateTime dtObj = DateTime.now(); | |
String getDay; | |
String getMonth; | |
String dt = dtObj.day.toString(); | |
String yr = dtObj.year.toString(); | |
// get day | |
switch (dtObj.weekday) { | |
case 1: | |
getDay = "Monday"; | |
break; | |
case 2: | |
getDay = "Tuesday"; | |
break; | |
case 3: | |
getDay = "Wednesday"; | |
break; | |
case 4: | |
getDay = "Thursday"; | |
break; | |
case 5: | |
getDay = "Friday"; | |
break; | |
case 6: | |
getDay = "Saturday"; | |
break; | |
default: | |
getDay = "Sunday"; | |
break; | |
} | |
// get month | |
switch (dtObj.month) { | |
case 1: | |
getMonth = "Jan"; | |
break; | |
case 2: | |
getMonth = "Feb"; | |
break; | |
case 3: | |
getMonth = "Mar"; | |
break; | |
case 4: | |
getMonth = "Apr"; | |
break; | |
case 5: | |
getMonth = "May"; | |
break; | |
case 6: | |
getMonth = "Jun"; | |
break; | |
case 7: | |
getMonth = "Jul"; | |
break; | |
case 8: | |
getMonth = "Aug"; | |
break; | |
case 9: | |
getMonth = "Sep"; | |
break; | |
case 10: | |
getMonth = "Oct"; | |
break; | |
case 11: | |
getMonth = "Nov"; | |
break; | |
default: | |
getMonth = "Dec"; | |
break; | |
} | |
return '$getDay, $getMonth $dt $yr'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment