Skip to content

Instantly share code, notes, and snippets.

@Vonarian
Last active September 15, 2025 13:06
Show Gist options
  • Save Vonarian/d769bb38afc5d1368cfe6e8b6eaba27e to your computer and use it in GitHub Desktop.
Save Vonarian/d769bb38afc5d1368cfe6e8b6eaba27e to your computer and use it in GitHub Desktop.
Time calculation
import 'dart:core';
// Define the start and end times
// These can now be "h:mm AM/PM" or "HH:mm"
String startTimeStr = "8:46 AM";
String endTimeStr = "5:30 PM";
// Example for 24-hour format:
// String startTimeStr = "08:46";
// String endTimeStr = "17:30";
// Example for mixed formats:
// String startTimeStr = "8:46 AM";
// String endTimeStr = "17:30";
/// Parses a time string, supporting both "h:mm AM/PM" (12-hour)
/// and "HH:mm" (24-hour) formats.
///
/// Returns a [DateTime] object with a fixed arbitrary date (Jan 1, 2025)
/// and the parsed time, or `null` if the string cannot be parsed.
DateTime? parseTime(String timeStr) {
try {
final timeStrUpper = timeStr.toUpperCase();
final is12HourFormat =
timeStrUpper.contains("AM") || timeStrUpper.contains("PM");
int hour;
int minute;
if (is12HourFormat) {
final parts = timeStr.split(" ");
if (parts.length != 2) return null; // Expected "time meridian"
final hmParts = parts[0].split(":");
if (hmParts.length != 2) return null; // Expected "h:mm"
hour = int.parse(hmParts[0]);
minute = int.parse(hmParts[1]);
String meridian = parts[1].toUpperCase();
if (meridian == "PM" && hour != 12) {
hour += 12;
} else if (meridian == "AM" && hour == 12) {
hour = 0; // 12 AM (midnight) is 00:xx in 24-hour format
}
// Basic validation for parsed hour/minute values
if (hour < 0 || hour > 23 || minute < 0 || minute > 59) {
return null;
}
} else {
// Assume 24-hour format (HH:mm)
final hmParts = timeStr.split(":");
if (hmParts.length != 2) return null; // Expected "HH:mm"
hour = int.parse(hmParts[0]);
minute = int.parse(hmParts[1]);
// Basic validation for parsed hour/minute values
if (hour < 0 || hour > 23 || minute < 0 || minute > 59) {
return null;
}
}
// Use a fixed date; only the time component is relevant for duration.
return DateTime(2025, 1, 1, hour, minute);
} on FormatException {
// Catch errors from int.parse if the number format is invalid
return null;
} catch (e) {
// Catch any other unexpected errors during parsing
return null;
}
}
void main() {
DateTime? startTime = parseTime(startTimeStr);
DateTime? endTime = parseTime(endTimeStr);
if (startTime == null) {
print(
"Error: Could not parse start time '$startTimeStr'. Please use 'h:mm AM/PM' or 'HH:mm' format.",
);
return;
}
if (endTime == null) {
print(
"Error: Could not parse end time '$endTimeStr'. Please use 'h:mm AM/PM' or 'HH:mm' format.",
);
return;
}
// Calculate difference
Duration difference = endTime.difference(startTime);
int totalMinutes = difference.inMinutes;
print(
"The number of minutes from $startTimeStr to $endTimeStr is $totalMinutes minutes.",
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment