12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- import 'package:date_format/date_format.dart';
- import 'package:intl/intl.dart';
- abstract class DataTimeUtils {
- /// 转成UTC字符串
- static String convertToUTCString(DateTime date) {
- // final utcFormatter = DateFormat('yyyy-MM-ddTHH:mm:ssZ');
- // final utcString = utcFormatter.format(date.toUtc());
- // return utcString;
- return formatDate(date.toUtc(),
- [yyyy, '-', mm, '-', dd, 'T', HH, ':', nn, ':', ss, ' ', Z]);
- }
- static String formatDateString(DateTime date) {
- return formatDate(date, [yyyy, '-', mm, '-', dd]);
- }
- /// UTC字符串转DateTime
- static DateTime convertFromUTCString(String utcString) {
- final utcFormatter = DateFormat('yyyy-MM-ddTHH:mm:ssZ');
- final dateTime = utcFormatter.parseUtc(utcString);
- return dateTime;
- }
- static DateTime convertFromDateString(String utcString) {
- final utcFormatter = DateFormat('yyyy-MM-dd');
- final dateTime = utcFormatter.parseUtc(utcString);
- return dateTime;
- }
- /// 计算年龄
- static String calculateAge(DateTime birthDate) {
- DateTime currentDate = DateTime.now();
- int age = currentDate.year - birthDate.year;
- if (currentDate.month < birthDate.month ||
- (currentDate.month == birthDate.month &&
- currentDate.day < birthDate.day)) {
- age--;
- }
- return age.toString();
- }
- }
|