Common Date and Time fomatters

This commit is contained in:
2026-02-21 08:32:07 +08:00
parent 4811621dc5
commit d32449d096
5 changed files with 90 additions and 53 deletions
+44
View File
@@ -1,3 +1,4 @@
import 'package:flutter/material.dart';
import 'package:timezone/data/latest.dart' as tz;
import 'package:timezone/timezone.dart' as tz;
@@ -27,4 +28,47 @@ class AppTime {
static DateTime parse(String value) {
return toAppTime(DateTime.parse(value));
}
/// Converts a [DateTime] into a human-readable short date string.
///
/// Example: **Jan 05, 2025**. This matches the format previously used by
/// `_formatDate` helpers across multiple screens.
static String formatDate(DateTime value) {
const months = [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec',
];
final month = months[value.month - 1];
final day = value.day.toString().padLeft(2, '0');
return '$month $day, ${value.year}';
}
/// Formats a [DateTimeRange] as ``start - end`` using [formatDate].
static String formatDateRange(DateTimeRange range) {
return '${formatDate(range.start)} - ${formatDate(range.end)}';
}
/// Renders a [DateTime] in 12hour clock notation with AM/PM suffix.
///
/// Example: **08:30 PM**. Used primarily in workforce-related screens.
static String formatTime(DateTime value) {
final rawHour = value.hour;
final hour = (rawHour % 12 == 0 ? 12 : rawHour % 12).toString().padLeft(
2,
'0',
);
final minute = value.minute.toString().padLeft(2, '0');
final suffix = rawHour >= 12 ? 'PM' : 'AM';
return '$hour:$minute $suffix';
}
}