Skip to content

Instantly share code, notes, and snippets.

@SEAbdulbasit
Created February 10, 2024 16:31
Show Gist options
  • Select an option

  • Save SEAbdulbasit/6074ccbd7da827dd008b355eb7e76581 to your computer and use it in GitHub Desktop.

Select an option

Save SEAbdulbasit/6074ccbd7da827dd008b355eb7e76581 to your computer and use it in GitHub Desktop.
@Composable
fun AgeCalculator() {
var startDate by remember { mutableStateOf(LocalDate.now()) }
var endDate by remember { mutableStateOf(LocalDate.now()) }
var showDialogStartDate by remember { mutableStateOf(false) }
var showDialogEndDate by remember { mutableStateOf(false) }
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
OutlinedTextField(
value = startDate.format(DateTimeFormatter.ofPattern("dd/MM/yyyy")),
onValueChange = {},
label = { Text("Date of birth") },
modifier = Modifier.clickable { showDialogStartDate = true },
readOnly = true,
)
if (showDialogStartDate) {
DatePickerDialog(startDate) { newDate ->
startDate = newDate
showDialogStartDate = false
}
}
Spacer(modifier = Modifier.height(16.dp))
OutlinedTextField(
value = endDate.format(DateTimeFormatter.ofPattern("dd/MM/yyyy")),
onValueChange = {},
label = { Text("End date") },
modifier = Modifier.clickable { showDialogEndDate = true },
readOnly = true,
)
if (showDialogEndDate) {
DatePickerDialog(endDate) { newDate ->
endDate = newDate
showDialogEndDate = false
}
}
Spacer(modifier = Modifier.height(16.dp))
Button(onClick = {
// val daysBetween = Date..between(startDate, endDate)
// Use an appropriate method to show the difference in days
// For example, if you have a SnackbarHostState, you could use:
// snackbarHostState.showSnackbar("Duration is $daysBetween days")
// println("Duration is $daysBetween days")
}) {
Text("Calculate age")
}
}
}
@Composable
fun DatePickerDialog(initialDate: LocalDate, onDateSelected: (LocalDate) -> Unit) {
}
@aadil-3699

Copy link
Copy Markdown

This Jetpack Compose implementation is a really clean way to handle date picking and state management for temporal inputs. Using LocalDate combined with mutable state parameters completely prevents state drifting when users select their birth date versus the target execution date.

If you are looking to expand the logic block inside the Button click listener to handle complex calendar intervals or localized format transformations (such as switching dynamically between different global calendar rules), it helps to cross-examine the calculation engine outputs with a live production setup. I used a similar programmatic routing approach to isolate chronological date arrays over at agecalculatorsa.com, and validating the UI state against a running production build helps catch timezone offset bugs early. For a complete implementation, adding a simple Period.between(startDate, endDate) method inside your click event will easily parse out the final years, months, and days format directly into your UI text field output. Nice work sharing this template snippet!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment