DateTimeFormatsVN
The DateTimeFormatsVN
class defines a set of standardized date and time formats tailored for the Vietnamese locale (vi_VN
). These constants are designed to work seamlessly with the intl
package and ensure consistent formatting across your app.
π§ Why use this?β
- Enforces locale-specific formatting.
- Centralizes all date/time format strings in one place.
- Improves consistency, reusability, and maintainability.
π¦ Usageβ
import 'package:intl/intl.dart';
final formatted = DateFormat(DateTimeFormatsVN.dateOnly, 'vi_VN').format(DateTime.now());
print(formatted); // e.g., 26/03/2025
π Available Formatsβ
Constant | Format String | Example |
---|---|---|
dateTime | dd/MM/yyyy HH:mm:ss | 01/01/2024 14:30:45 |
dateTimeWithoutSeconds | dd/MM/yyyy HH:mm | 01/01/2024 14:30 |
dateOnly | dd/MM/yyyy | 01/01/2024 |
timeOnly | HH:mm:ss | 14:30:45 |
timeOnlyWithoutSeconds | HH:mm | 14:30 |
dateWithDayAndMonthOnly | dd/MM | 01/01 |
yearOnly | yyyy | 2024 |
dateWithDayName | EEEE, dd/MM/yyyy | Thα»© Hai, 01/01/2024 |
dateWithMonthName | dd MMMM yyyy | 01 ThΓ‘ng Mα»t 2024 |
shortDateWithMonthName | dd MMM | 01 ThΓ‘ng Mα»t |
β Best Practicesβ
- Use these constants throughout your app to avoid hardcoding format strings.
- Always pass
"vi_VN"
explicitly to theDateFormat
constructor to ensure proper localization. - Use
DateFormat(...).format(...)
instead of.toString()
for user-facing strings.
π Locationβ
This class is defined in:
lib/constants/constants.dart
It is marked with part of "constants.dart";
, so ensure your constants.dart
file includes:
part 'date_time_formats_vn.dart';