46 lines
1.2 KiB
Dart
46 lines
1.2 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:intl/intl.dart';
|
|
|
|
/// Eine Service-Klasse um mit Datums-Werten umzugehen
|
|
class DateService {
|
|
/// Die Standard-Formatierung von einem Datum
|
|
static final DateFormat dateFormat = DateFormat('dd.MM.yyyy');
|
|
|
|
/// Wandelt einen String in eine DateTimeRange um
|
|
static DateTimeRange? stringToDateTimeRange(final String? value) {
|
|
if (value == null || value.trim().isEmpty) {
|
|
return null;
|
|
}
|
|
|
|
final List<String> parts = value.split(' - ');
|
|
if (parts.length != 2) {
|
|
return null;
|
|
}
|
|
|
|
try {
|
|
final DateTime start = DateService.dateFormat.parseStrict(
|
|
parts[0].trim(),
|
|
);
|
|
final DateTime end = DateService.dateFormat.parseStrict(parts[1].trim());
|
|
|
|
if (end.isBefore(start)) {
|
|
return null;
|
|
}
|
|
|
|
return DateTimeRange(start: start, end: end);
|
|
} on FormatException catch (_) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// Wandelt eine DateTimeRange in einen String um
|
|
static String? dateTimeRangeToString(final DateTimeRange? value) {
|
|
if (value != null) {
|
|
return '${dateFormat.format(value.start)}'
|
|
' - '
|
|
'${dateFormat.format(value.end)}';
|
|
}
|
|
return null;
|
|
}
|
|
}
|