Member-only story
import 'package:intl/intl.dart';
// const String dateString = '10/12/2020';
// final DateTime dateTimeObj = DateFormat('dd/MM/yyyy').parse(dateString);
// print('dateTimeObj $dateTimeObj');
//
// final String dateTimeFormatString = DateFormat('yyyy-MM-dd').format(dateTimeObj);
// print('dateTimeFormatString $dateTimeFormatString');
// Today 11:08 PM
// => 'Today ${DateFormat('h:mm a').format(toDay)}'
//
// Yesterday 11:08 PM
// => 'Yesterday ${DateFormat('h:mm a').format(yesterDay)}'
//
// Tuesday 11:08 PM
// => DateFormat('EEEE h:mm a').format(weekD2)
//
// Wed, Jul 29, 11:08 PM
// => DateFormat('E, MMM dd, h:mm a').format(yearDay)
//
// Jul 25, 2019, 11:08 PM
// => DateFormat('MMM dd, yyyy, h:mm a').format(otherDay)
/// Extension for DateTime
extension DateTimeExtension on DateTime {
// Convert DateTime to String
// 2020-04-03T11:57:00
String toDateTimeString() {
return DateFormat('yyyy-MM-ddThh:mm:ss').format(this);
}
// Check this is now
bool isToday(DateTime now) {
final DateTime today = DateTime(now.year, now.month, now.day);
final DateTime dayCheck = DateTime(year, month, day);
return today == dayCheck;
}
// Check this is yesterday
bool isYesterday(DateTime now) {
final DateTime yesterday = DateTime(now.year, now.month, now.day - 1);
final DateTime dayCheck = DateTime(year, month, day);
return yesterday == dayCheck;
}
// Check if this is in current week
bool isWeekday(DateTime now) {
final DateTime firstDayOfTheWeek =
now.subtract(Duration(days: now.weekday));
return isAfter(firstDayOfTheWeek);
}
/// 1+ hours difference between current time and the old one.
bool isOver1Hour(DateTime prevTime) {
return !prevTime.add(const Duration(hours: 1)).isAfter(this);
}
}
/// Extension for DateTime from String
extension DateTimeStringExtendsion on String {
// Parse DateTime from String
// 2020-04-03T11:57:00
DateTime toDateTime() {
return DateFormat('=yyyy-MM-ddThh:mm:ss').parse(this);
}
}