Created
September 28, 2020 11:04
-
-
Save panuavakul/2559e02684dcef3c0781193900deb704 to your computer and use it in GitHub Desktop.
Example for extending DateTime
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:cloud_firestore/cloud_firestore.dart'; | |
import 'package:intl/intl.dart'; | |
/// Just a normal [DateTime] with helper functions | |
class TabetaDateTime extends DateTime { | |
TabetaDateTime(int year, | |
[int month = 1, | |
int day = 1, | |
int hour = 0, | |
int minute = 0, | |
int second = 0, | |
int millisecond = 0, | |
int microsecond = 0]) | |
: super(year, month, day, hour, minute, second, millisecond, microsecond); | |
TabetaDateTime.utc(int year, | |
[int month = 1, | |
int day = 1, | |
int hour = 0, | |
int minute = 0, | |
int second = 0, | |
int millisecond = 0, | |
int microsecond = 0]) | |
: super.utc( | |
year, month, day, hour, minute, second, millisecond, microsecond); | |
TabetaDateTime.now() : super.now(); | |
TabetaDateTime.fromDateTime(DateTime dateTime) | |
: super( | |
dateTime.year, | |
dateTime.month, | |
dateTime.day, | |
dateTime.hour, | |
dateTime.minute, | |
dateTime.second, | |
dateTime.millisecond, | |
dateTime.microsecond, | |
); | |
factory TabetaDateTime.fromTimestamp(Timestamp timestamp) { | |
return TabetaDateTime.fromDateTime(timestamp.toDate()); | |
} | |
factory TabetaDateTime.fromData(dynamic data) { | |
return TabetaDateTime.fromTimestamp(data as Timestamp); | |
} | |
DateTime toDateTime() { | |
return DateTime( | |
year, month, day, hour, minute, second, millisecond, microsecond); | |
} | |
Timestamp toTimestamp() { | |
return Timestamp.fromDate(this); | |
} | |
TabetaDateTime startOfDay() { | |
return TabetaDateTime(year, month, day); | |
} | |
TabetaDateTime endOfDay() { | |
return TabetaDateTime(year, month, day, 23, 59, 59); | |
} | |
TabetaDateTime startOfNextDay() { | |
return TabetaDateTime(year, month, day + 1); | |
} | |
TabetaDateTime yesterday() { | |
return TabetaDateTime.fromDateTime(subtract(const Duration(days: 1))); | |
} | |
TabetaDateTime tomorrow() { | |
return TabetaDateTime.fromDateTime(add(const Duration(days: 1))); | |
} | |
TabetaDateTime lastWeek() { | |
return TabetaDateTime.fromDateTime(subtract(const Duration(days: 7))); | |
} | |
TabetaDateTime lastMonth() { | |
return TabetaDateTime.fromDateTime(subtract(const Duration(days: 30))); | |
} | |
String formatM() { | |
return DateFormat.M().format(this); | |
} | |
String formatMd() { | |
return DateFormat.Md().format(this); | |
} | |
String formatHm() { | |
return DateFormat.Hm().format(this); | |
} | |
bool isToday() { | |
return isSameDay(TabetaDateTime.now()); | |
} | |
bool isYesterday() { | |
return isSameDay(TabetaDateTime.now().yesterday()); | |
} | |
bool isSameDay(DateTime dateTime) { | |
final tabetaDateTime = TabetaDateTime.fromDateTime(dateTime); | |
return isAfter(tabetaDateTime.startOfDay()) && | |
isBefore(tabetaDateTime.endOfDay()); | |
} | |
bool isAfterOrSame(DateTime other) { | |
return isAfter(other) || isAtSameMomentAs(other); | |
} | |
bool isBeforeOrSame(DateTime other) { | |
return isBefore(other) || isAtSameMomentAs(other); | |
} | |
TabetaDateTime startOfWeek() { | |
return TabetaDateTime.fromDateTime( | |
subtract(Duration(days: weekday - 1)), | |
).startOfDay(); | |
} | |
TabetaDateTime endOfWeek() { | |
return TabetaDateTime.fromDateTime(add(Duration(days: 7 - weekday))) | |
.endOfDay(); | |
} | |
TabetaDateTime startOfMonth() { | |
return TabetaDateTime( | |
year, | |
month, | |
1, | |
0, | |
0, | |
0, | |
0, | |
0, | |
); | |
} | |
TabetaDateTime endOfMonth() { | |
final endOfMonth = TabetaDateTime( | |
year, | |
month + 1, | |
1, | |
0, | |
0, | |
0, | |
0, | |
0, | |
).subtract(const Duration(days: 1)); | |
return TabetaDateTime.fromDateTime(endOfMonth); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment