DateTime Extension
tryToDateTime
Safely attempts to convert a string to a DateTime object. Returns null if the conversion fails.
String? dateStr = "2023-01-01";
print(dateStr.tryToDateTime); // Output: DateTime object or nullString? dateStr = "2023-01-01";
print(dateStr.tryToDateTime); // Output: DateTime object or nulltoDateTime
Converts a string to a DateTime object. Throws an exception if the conversion fails.
String dateStr = "2023-01-01";
print(dateStr.toDateTime); // Output: DateTime objectString dateStr = "2023-01-01";
print(dateStr.toDateTime); // Output: DateTime objectdateFormat
Returns a DateFormat object based on the string format provided.
String format = "yMd";
print(format.dateFormat); // Output: DateFormat objectString format = "yMd";
print(format.dateFormat); // Output: DateFormat objecttoDateWithFormat
Converts a string to a DateTime object using a specific format.
String dateStr = "1-2-2023";
print(dateStr.toDateWithFormat("d-M-y")); // Output: DateTime objectString dateStr = "1-2-2023";
print(dateStr.toDateWithFormat("d-M-y")); // Output: DateTime objecttryToDateWithFormat
Safely attempts to convert a string to a DateTime object using a specific format. Returns null if the conversion fails.
String? dateStr = "1-2-2023";
print(dateStr.tryToDateWithFormat("d-M-y")); // Output: DateTime object or nullString? dateStr = "1-2-2023";
print(dateStr.tryToDateWithFormat("d-M-y")); // Output: DateTime object or nulltimestampToDate
Converts a timestamp string to a DateTime object.
String timestamp = "1635530400000";
print(timestamp.timestampToDate); // Output: DateTime objectString timestamp = "1635530400000";
print(timestamp.timestampToDate); // Output: DateTime objecttoSmallMonthName and toFullMonthName
Convert an integer representing a month to its abbreviated or full name.
int month = 1;
print(month.toSmallMonthName); // Output: "Jan"
print(month.toFullMonthName); // Output: "January"int month = 1;
print(month.toSmallMonthName); // Output: "Jan"
print(month.toFullMonthName); // Output: "January"toSmallDayName and toFullDayName
Convert an integer representing a day of the week to its abbreviated or full name.
int day = 1;
print(day.toSmallDayName); // Output: "Mon"
print(day.toFullDayName); // Output: "Monday"int day = 1;
print(day.toSmallDayName); // Output: "Mon"
print(day.toFullDayName); // Output: "Monday"timestampToDate (numeric)
Converts a numeric timestamp to a DateTime object.
int timestamp = 1635530400000;
print(timestamp.timestampToDate); // Output: DateTime objectint timestamp = 1635530400000;
print(timestamp.timestampToDate); // Output: DateTime objectlocal
Returns a new DateTime object representing the local date and time.
DateTime? date = DateTime.utc(2023, 1, 1);
print(date.local); // Output: DateTime object in local timeDateTime? date = DateTime.utc(2023, 1, 1);
print(date.local); // Output: DateTime object in local timetoUtcIso
Returns an ISO-8601 string representation of the UTC date.
DateTime? date = DateTime.utc(2023, 1, 1);
print(date.toUtcIso); // Output: "2023-01-01T00:00:00.000Z"DateTime? date = DateTime.utc(2023, 1, 1);
print(date.toUtcIso); // Output: "2023-01-01T00:00:00.000Z"isTomorrow
Checks if the date is tomorrow.
DateTime date = DateTime.now().add(Duration(days: 1));
print(date.isTomorrow); // Output: trueDateTime date = DateTime.now().add(Duration(days: 1));
print(date.isTomorrow); // Output: trueisToday
Checks if the date is today.
DateTime date = DateTime.now();
print(date.isToday); // Output: trueDateTime date = DateTime.now();
print(date.isToday); // Output: trueisYesterday
Checks if the date was yesterday.
DateTime date = DateTime.now().subtract(Duration(days: 1));
print(date.isYesterday); // Output: trueDateTime date = DateTime.now().subtract(Duration(days: 1));
print(date.isYesterday); // Output: trueisPresent
Checks if the date is in the future.
DateTime date = DateTime.now().add(Duration(days: 1));
print(date.isPresent); // Output: trueDateTime date = DateTime.now().add(Duration(days: 1));
print(date.isPresent); // Output: trueisPast
Checks if the date is in the past.
DateTime date = DateTime.now().subtract(Duration(days: 1));
print(date.isPast); // Output: trueDateTime date = DateTime.now().subtract(Duration(days: 1));
print(date.isPast); // Output: trueisInPastWeek
Checks if the date is within the last week.
DateTime date = DateTime.now().subtract(Duration(days: 3));
print(date.isInPastWeek); // Output: trueDateTime date = DateTime.now().subtract(Duration(days: 3));
print(date.isInPastWeek); // Output: trueisInThisYear
Checks if the date is in the current year.
DateTime date = DateTime.now();
print(date.isInThisYear); // Output: trueDateTime date = DateTime.now();
print(date.isInThisYear); // Output: trueisFirstDayOfMonth
Checks if the date is the first day of the month.
DateTime date = DateTime(DateTime.now().year, DateTime.now().month, 1);
print(date.isFirstDayOfMonth); // Output: trueDateTime date = DateTime(DateTime.now().year, DateTime.now().month, 1);
print(date.isFirstDayOfMonth); // Output: trueisLastDayOfMonth
Checks if the date is the last day of the month.
DateTime date = DateTime(DateTime.now().year, DateTime.now().month + 1, 0);
print(date.isLastDayOfMonth); // Output: trueDateTime date = DateTime(DateTime.now().year, DateTime.now().month + 1, 0);
print(date.isLastDayOfMonth); // Output: trueisLeapYear
Checks if the year of the date is a leap year.
DateTime date = DateTime(2024, 1, 1);
print(date.isLeapYear); // Output: trueDateTime date = DateTime(2024, 1, 1);
print(date.isLeapYear); // Output: trueformat and tryFormat
format a DateTime object as a string using a specified format, while tryFormat attempts to do the same but returns null if unsuccessful.
DateTime date = DateTime.now();
print(date.format("yMd")); // Output: "2023-01-01"
print(date.tryFormat("yMd")); // Output: "2023-01-01" or nullDateTime date = DateTime.now();
print(date.format("yMd")); // Output: "2023-01-01"
print(date.tryFormat("yMd")); // Output: "2023-01-01" or nullpassedDuration
Gets the duration that has passed since the date.
DateTime date = DateTime.now().subtract(Duration(days: 1));
print(date.passedDuration); // Output: Duration objectDateTime date = DateTime.now().subtract(Duration(days: 1));
print(date.passedDuration); // Output: Duration objectremainingDuration
Gets the duration remaining until the date.
DateTime date = DateTime.now().add(Duration(days: 1));
print(date.remainingDuration); // Output: Duration objectDateTime date = DateTime.now().add(Duration(days: 1));
print(date.remainingDuration); // Output: Duration objectpassedDays
Gets the number of days that have passed since the date.
DateTime date = DateTime.now().subtract(Duration(days: 1));
print(date.passedDays); // Output: 1DateTime date = DateTime.now().subtract(Duration(days: 1));
print(date.passedDays); // Output: 1remainingDays
Gets the number of days remaining until the date.
DateTime date = DateTime.now().add(Duration(days: 1));
print(date.remainingDays); // Output: 1DateTime date = DateTime.now().add(Duration(days: 1));
print(date.remainingDays); // Output: 1+ (Addition)
Adds a Duration to the DateTime object.
DateTime date = DateTime.now();
Duration duration = Duration(days: 1);
print(date + duration); // Output: DateTime objectDateTime date = DateTime.now();
Duration duration = Duration(days: 1);
print(date + duration); // Output: DateTime object- (Subtraction)
Subtracts a Duration from the DateTime object.
DateTime date = DateTime.now();
Duration duration = Duration(days: 1);
print(date - duration); // Output: DateTime objectDateTime date = DateTime.now();
Duration duration = Duration(days: 1);
print(date - duration); // Output: DateTime objectisAtSameYearAs
Checks if the given DateTime object is in the same year as the current DateTime object. This function does not account for timezones.
DateTime dateTime1 = DateTime(2022, 6, 15);
DateTime dateTime2 = DateTime(2022, 1, 5);
print(dateTime1.isAtSameYearAs(dateTime2)); // Output: trueDateTime dateTime1 = DateTime(2022, 6, 15);
DateTime dateTime2 = DateTime(2022, 1, 5);
print(dateTime1.isAtSameYearAs(dateTime2)); // Output: trueisAtSameMonthAs
Checks if the given DateTime object is in the same month and year as the current DateTime object. This function does not account for timezones.
DateTime dateTime1 = DateTime(2022, 6, 15);
DateTime dateTime2 = DateTime(2022, 6
, 5);
print(dateTime1.isAtSameMonthAs(dateTime2)); // Output: trueDateTime dateTime1 = DateTime(2022, 6, 15);
DateTime dateTime2 = DateTime(2022, 6
, 5);
print(dateTime1.isAtSameMonthAs(dateTime2)); // Output: trueisAtSameDayAs
Checks if the given DateTime object is on the same day, month, and year as the current DateTime object. This function does not account for timezones.
DateTime dateTime1 = DateTime(2022, 6, 15);
DateTime dateTime2 = DateTime(2022, 6, 15);
print(dateTime1.isAtSameDayAs(dateTime2)); // Output: trueDateTime dateTime1 = DateTime(2022, 6, 15);
DateTime dateTime2 = DateTime(2022, 6, 15);
print(dateTime1.isAtSameDayAs(dateTime2)); // Output: trueisAtSameHourAs
Checks if the given DateTime object is at the same hour, day, month, and year as the current DateTime object. This function does not account for timezones.
DateTime dateTime1 = DateTime(2022, 6, 15, 12);
DateTime dateTime2 = DateTime(2022, 6, 15, 12);
print(dateTime1.isAtSameHourAs(dateTime2)); // Output: trueDateTime dateTime1 = DateTime(2022, 6, 15, 12);
DateTime dateTime2 = DateTime(2022, 6, 15, 12);
print(dateTime1.isAtSameHourAs(dateTime2)); // Output: trueisAtSameMinuteAs, isAtSameSecondAs, isAtSameMillisecondAs, isAtSameMicrosecondAs
These methods provide finer granularity checks, ranging from minutes to microseconds.
startOfDay, startOfMonth, startOfYear
These methods return a new DateTime object representing the start of the day, month, or year of the original DateTime object.
DateTime dateTime = DateTime(2022, 6, 15, 12, 30);
print(dateTime.startOfDay); // Output: 2022-06-15 00:00:00.000DateTime dateTime = DateTime(2022, 6, 15, 12, 30);
print(dateTime.startOfDay); // Output: 2022-06-15 00:00:00.000tomorrow, yesterday
These methods return a new DateTime object representing the next or previous day.
DateTime dateTime = DateTime(2022, 6, 15);
print(dateTime.tomorrow); // Output: 2022-06-16 00:00:00.000
print(dateTime.yesterday); // Output: 2022-06-14 00:00:00.000DateTime dateTime = DateTime(2022, 6, 15);
print(dateTime.tomorrow); // Output: 2022-06-16 00:00:00.000
print(dateTime.yesterday); // Output: 2022-06-14 00:00:00.000addOrRemoveYears, addOrRemoveMonth, addOrRemoveDay, addOrRemoveMinutes, addOrRemoveSeconds
These methods allow you to add or remove a certain time unit from the DateTime object. To remove, pass a negative number.
DateTime dateTime = DateTime(2022, 6, 15);
print(dateTime.addOrRemoveYears(1)); // Output: 2023-06-15 00:00:00.000DateTime dateTime = DateTime(2022, 6, 15);
print(dateTime.addOrRemoveYears(1)); // Output: 2023-06-15 00:00:00.000min, max
Returns the smaller or larger date between the current and another DateTime object.
DateTime dateTime1 = DateTime(2022, 6, 15);
DateTime dateTime2 = DateTime(2021, 6, 15);
print(dateTime1.min(dateTime2)); // Output: 2021-06-15 00:00:00.000DateTime dateTime1 = DateTime(2022, 6, 15);
DateTime dateTime2 = DateTime(2021, 6, 15);
print(dateTime1.min(dateTime2)); // Output: 2021-06-15 00:00:00.000addDays, addHours
These methods add a certain number of days or hours to the DateTime object.
DateTime dateTime = DateTime(2022, 6, 15);
print(dateTime.addDays(1)); // Output: 2022-06-16 00:00:00.000DateTime dateTime = DateTime(2022, 6, 15);
print(dateTime.addDays(1)); // Output: 2022-06-16 00:00:00.000