Skip to content

Instantly share code, notes, and snippets.

@Rohit-554
Created February 13, 2025 11:30
Show Gist options
  • Select an option

  • Save Rohit-554/28314a204be45b040c90e757f7e5d791 to your computer and use it in GitHub Desktop.

Select an option

Save Rohit-554/28314a204be45b040c90e757f7e5d791 to your computer and use it in GitHub Desktop.
// Get epoch time
const val formatPattern = "yyyy-MM-dd'T'HH:mm:ss[.SSS]"
@OptIn(FormatStringsInDatetimeFormats::class)
private val EpochDateTimeFormatter = LocalDateTime.Format {
byUnicodePattern(formatPattern)
}
fun getFormattedDate(timeStamp: Long): String {
return EpochDateTimeFormatter.format(
Instant.fromEpochMilliseconds(timeStamp)
.toLocalDateTime(TimeZone.currentSystemDefault())
)
}
// Format the epoch time
fun formatIsoStringToCustomDate(isoString: String): String {
val localDateTime = LocalDateTime.parse(isoString)
val day = localDateTime.dayOfMonth
val month = localDateTime.month.name.lowercase().replaceFirstChar { it.uppercase() }.take(3)
val year = localDateTime.year
// Return the formatted string
return "$day $month $year"
}
-------------------------------------------------------------------------------------------------------------------------------
// Utilise the Extension Function
@OptIn(ExperimentalMaterial3Api::class, FormatStringsInDatetimeFormats::class)
fun Long.getFormattedDate(): String {
val localDateTime =
Instant.
fromEpochMilliseconds(this)
.toLocalDateTime(TimeZone.currentSystemDefault())
val day =
localDateTime.dayOfMonth
val month =
localDateTime.month.name.lowercase().
replaceFirstChar { it.uppercase() }.take(3)
val year =
localDateTime.year
return "$day $month $year"
}
//usage
val endDateFormatted = endDatePickerState.selectedDateMillis?.getFormattedDate()
//output 13 Feb 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment