Carbon class
The Carbon Class - Laravel-Style Date Wrapper.
Provides a fluent, expressive API for date manipulation without exposing the underlying Jiffy engine to developers.
Usage
// Create instances
final now = Carbon.now();
final parsed = Carbon.parse('2024-01-15');
final fromDt = Carbon.fromDateTime(DateTime.now());
// Manipulation (returns new instance)
final tomorrow = now.addDay();
final nextMonth = now.addMonths(1);
final startOfMonth = now.startOfMonth();
// Formatting
now.format('yyyy-MM-dd'); // "2024-01-15"
now.diffForHumans(); // "2 hours ago"
// Comparison
now.isToday(); // true
now.isFuture(); // false
- Implemented types
Constructors
- Carbon.create({int year = 1970, int month = 1, int day = 1, int hour = 0, int minute = 0, int second = 0, int millisecond = 0})
-
Create a Carbon instance from specific date/time parts.
factory
- Carbon.fromDateTime(DateTime dateTime)
-
Create a Carbon instance from a DateTime.
factory
- Carbon.now([String? timezone])
-
Create a Carbon instance for the current date/time.
factory
- Carbon.parse(String date)
-
Parse a date string into a Carbon instance.
factory
Properties
- day → int
-
Get the day of month.
no setter
- dayOfWeek → int
-
Get the day of week (1 = Monday, 7 = Sunday).
no setter
- dayOfYear → int
-
Get the day of year (1-366).
no setter
- daysInMonth → int
-
Get the number of days in the current month.
no setter
- hashCode → int
-
The hash code for this object.
no setteroverride
- hour → int
-
Get the hour (0-23).
no setter
- microsecondsSinceEpoch → int
-
Get microseconds since epoch.
no setter
- millisecond → int
-
Get the millisecond.
no setter
- millisecondsSinceEpoch → int
-
Get milliseconds since epoch.
no setter
- minute → int
-
Get the minute (0-59).
no setter
- month → int
-
Get the month (1-12).
no setter
- quarter → int
-
Get the quarter (1-4).
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- second → int
-
Get the second (0-59).
no setter
- timeZoneName → String
-
Get the timezone name.
no setter
- toDateTime → DateTime
-
Get the underlying DateTime.
no setter
- weekOfYear → int
-
Get the week of year.
no setter
- year → int
-
Get the year.
no setter
Methods
-
add(
Duration duration) → Carbon - Add a duration.
-
addDay(
) → Carbon - Add one day.
-
addDays(
int value) → Carbon - Add days.
-
addHours(
int value) → Carbon - Add hours.
-
addMinutes(
int value) → Carbon - Add minutes.
-
addMonth(
) → Carbon - Add one month.
-
addMonths(
int value) → Carbon - Add months.
-
addSeconds(
int value) → Carbon - Add seconds.
-
addWeek(
) → Carbon - Add one week.
-
addWeeks(
int value) → Carbon - Add weeks.
-
addYear(
) → Carbon - Add one year.
-
addYears(
int value) → Carbon - Add years.
-
compareTo(
Carbon other) → int -
Compares this object to another object.
override
-
copy(
) → Carbon - Create a copy of this Carbon instance.
-
diffForHumans(
[Carbon? other]) → String - Get human-readable difference (e.g., "2 hours ago").
-
diffInDays(
Carbon other) → int - Get difference in days.
-
diffInHours(
Carbon other) → int - Get difference in hours.
-
diffInMinutes(
Carbon other) → int - Get difference in minutes.
-
diffInMonths(
Carbon other) → int - Get difference in months.
-
diffInSeconds(
Carbon other) → int - Get difference in seconds.
-
diffInYears(
Carbon other) → int - Get difference in years.
-
endOfDay(
) → Carbon - Get end of day (23:59:59.999).
-
endOfMonth(
) → Carbon - Get end of month.
-
endOfWeek(
) → Carbon - Get end of week.
-
endOfYear(
) → Carbon - Get end of year.
-
format(
String pattern) → String - Format the date with a custom pattern.
-
isAfter(
Carbon other) → bool - Check if this date is after another.
-
isBefore(
Carbon other) → bool - Check if this date is before another.
-
isBetween(
Carbon start, Carbon end, [Unit unit = Unit.day]) → bool - Check if this date is between two others.
-
isFuture(
) → bool - Check if this date is in the future.
-
isLeapYear(
) → bool - Check if this is a leap year.
-
isPast(
) → bool - Check if this date is in the past.
-
isSame(
Carbon other, [Unit unit = Unit.day]) → bool - Check if this date is the same as another.
-
isSameOrAfter(
Carbon other, [Unit unit = Unit.day]) → bool - Check if this date is the same or after another.
-
isSameOrBefore(
Carbon other, [Unit unit = Unit.day]) → bool - Check if this date is the same or before another.
-
isToday(
) → bool - Check if this is today.
-
isTomorrow(
) → bool - Check if this is tomorrow.
-
isWeekday(
) → bool - Check if this is a weekday.
-
isWeekend(
) → bool - Check if this is a weekend.
-
isYesterday(
) → bool - Check if this is yesterday.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
setTimezone(
String timezone) → Carbon - Convert to a different timezone.
-
startOfDay(
) → Carbon - Get start of day (00:00:00).
-
startOfMonth(
) → Carbon - Get start of month.
-
startOfWeek(
) → Carbon - Get start of week.
-
startOfYear(
) → Carbon - Get start of year.
-
subDay(
) → Carbon - Subtract one day.
-
subDays(
int value) → Carbon - Subtract days.
-
subHours(
int value) → Carbon - Subtract hours.
-
subMinutes(
int value) → Carbon - Subtract minutes.
-
subMonth(
) → Carbon - Subtract one month.
-
subMonths(
int value) → Carbon - Subtract months.
-
subSeconds(
int value) → Carbon - Subtract seconds.
-
subtract(
Duration duration) → Carbon - Subtract a duration.
-
subWeek(
) → Carbon - Subtract one week.
-
subWeeks(
int value) → Carbon - Subtract weeks.
-
subYear(
) → Carbon - Subtract one year.
-
subYears(
int value) → Carbon - Subtract years.
-
toDateString(
) → String - Get date string (yyyy-MM-dd).
-
toDateTimeString(
) → String - Get date time string (yyyy-MM-dd HH:mm:ss).
-
toFormattedDateString(
) → String - Get formatted date using config pattern.
-
toIso8601String(
) → String - Get ISO 8601 string.
-
toString(
) → String -
A string representation of this object.
override
-
toTimeString(
) → String - Get time string (HH:mm:ss).
Operators
-
operator ==(
Object other) → bool -
The equality operator.
override