50 lines
1.5 KiB
Dart
50 lines
1.5 KiB
Dart
import 'package:diameter/config.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:intl/intl.dart';
|
|
|
|
class DateTimeUtils {
|
|
static String displayDateTime(DateTime? date, {String fallback = ''}) {
|
|
if (date == null) {
|
|
return fallback;
|
|
}
|
|
DateTime localDate = date.toLocal();
|
|
final DateFormat formatter = DateFormat('$dateFormat $timeFormat');
|
|
return formatter.format(localDate);
|
|
}
|
|
|
|
static String displayDate(DateTime? date, {String fallback = ''}) {
|
|
if (date == null) {
|
|
return fallback;
|
|
}
|
|
DateTime localDate = date.toLocal();
|
|
final DateFormat formatter = DateFormat(longDateFormat ?? dateFormat);
|
|
return formatter.format(localDate);
|
|
}
|
|
|
|
static String displayTime(DateTime? date,
|
|
{String fallback = '', bool? longFormat}) {
|
|
if (date == null) {
|
|
return fallback;
|
|
}
|
|
DateTime localDate = date.toLocal();
|
|
final DateFormat formatter = DateFormat(
|
|
longFormat == true ? longTimeFormat ?? timeFormat : timeFormat);
|
|
return formatter.format(localDate);
|
|
}
|
|
|
|
static String displayTimeOfDay(TimeOfDay? time,
|
|
{String fallback = '', bool? longFormat}) {
|
|
if (time == null) {
|
|
return fallback;
|
|
}
|
|
final DateFormat formatter = DateFormat(
|
|
longFormat == true ? longTimeFormat ?? timeFormat : timeFormat);
|
|
return formatter.format(convertTimeOfDayToDateTime(time));
|
|
}
|
|
|
|
static DateTime convertTimeOfDayToDateTime(TimeOfDay time) {
|
|
return DateTime(
|
|
dummyDate.year, dummyDate.month, dummyDate.day, time.hour, time.minute);
|
|
}
|
|
}
|