Last active
August 7, 2022 13:48
-
-
Save carolinemusyoka/c30c3fb934cae954843c40ff18585f57 to your computer and use it in GitHub Desktop.
convert date
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
fun String.convertDate(context: Context): String? { | |
return try{ | |
// "2021-11=10T16:24:21.579537Z" | |
val currentLocale = ConfigurationCompat.getLocales(context.resources.configuration)[0] | |
val inputFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault()) | |
inputFormat.timeZone = TimeZone.getTimeZone("GMT") | |
val passedDate: Date = inputFormat.parse(this) as Date | |
// Form example Mon, Nov *, 2021, 12:10 pm | |
val outputFormatDay = SimpleDateFormat("EEE, MMM d, yyyy, hh:mm aaa", currentLocale) | |
outputFormatDay.timeZone = TimeZone.getDefault() | |
val newDateString = outputFormatDay.format(passedDate) | |
newDateString | |
} catch(_: Exception){ | |
"00:00:00" | |
} | |
} | |
// credits, @ronnieotieno |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment