DateTimeUtils class

  • @Author: chuxiong
  • @Created at: 23-07-2025 16:58
  • @Email:
  • description 日期时间工具:格式化、时长精度拆解、按模式解析、上下午与生肖星座判定、 星期 / 年内日序、闰年判定、今日 / 昨日 / 本周判定, 以及按日 / 周 / 月 / 年的边界与步进计算。纯静态方法,无状态。

Properties

hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited

Static Methods

addMonths(DateTime date, int months) → DateTime
Returns the DateTime resulting from adding the given number of months to this DateTime.
copyWith(DateTime date, {int? year, int? month, int? day, int? hour, int? minute, int? second, int? millisecond, int? microsecond}) → DateTime
Creates a copy of date but with the given fields replaced with the new values.
firstDayOfFirstWeek(int year, {int firstWeekday = DateTime.monday}) → DateTime
Returns start of the first day of the first week in year.
firstDayOfMonth(DateTime date) → DateTime
Returns DateTime that represents a beginning of the first day of the month containing date.
firstDayOfNextMonth(DateTime dateTime) → DateTime
Returns DateTime that represents a beginning of the first day of the next month.
firstDayOfNextWeek(DateTime dateTime, {int firstWeekday = DateTime.monday}) → DateTime
Returns start of the first day of the next week for specified dateTime.
firstDayOfNextYear(DateTime dateTime) → DateTime
Returns DateTime that represents a beginning of the first day of the next year.
firstDayOfWeek(DateTime dateTime, {int firstWeekday = DateTime.monday}) → DateTime
Returns start of the first day of the week for specified dateTime.
firstDayOfYear(DateTime dateTime) → DateTime
Returns DateTime that represents a beginning of the first day of the year containing date.
formatDate(DateTime? dateTime, {String? format}) → String
format date by DateTime. format 转换格式(已提供常用格式 DateFormats,可以自定义格式:'yyyy/MM/dd HH:mm:ss') 格式要求 year -> yyyy/yy month -> MM/M day -> dd/d hour -> HH/H minute -> mm/m second -> ss/s
formatDateMs(int ms, {bool isUtc = false, String? format}) → String
format date by milliseconds. 格式化日期毫秒时间
formatDateStr(String dateStr, {bool? isUtc, String? format}) → String
format date by date str. dateStr 日期字符串
formatFitTimeSpan(int millis, {int precision = 5, List<String>? units}) → String?
将毫秒时长 millis 按天/时/分/秒/毫秒拆解为可读串。
generateWithDayStep(DateTime start, DateTime end) → Iterable<DateTime>
Returns an iterable of DateTime with 1 day step in given range.
getBusinessDate({required DateTime checkoutTime, String? cutoffTime}) → DateTime
营业日归属:按日切线 cutoffTime 把结账时刻 checkoutTime 归属到 所属营业日的锚点时刻。
getChineseZodiac(DateTime dateTime) → String
生肖:按公历年粗判(年份 % 12,从「猴」起)。
getChineseZodiacByYear(int year) → String
生肖:按公历年份 year 粗判(year % 12,从「猴」起)。
getDateMsByTimeStr(String dateStr, {bool? isUtc, String? format}) → int?
get DateMilliseconds By DateStr. 将字符串时间转化为毫秒值
getDateTime(String dateStr, {bool? isUtc, String? format}) → DateTime?
get DateTime By DateStr. 将字符串时间转化为DateTime
getDateTimeByMs(int ms, {bool isUtc = false}) → DateTime
get DateTime By Milliseconds. 将毫秒时间转化为DateTime
getDayNumberInWeek(DateTime date, {int firstWeekday = DateTime.monday}) → int
Returns number of the day in week (starting with 1).
getDayNumberInYear(DateTime date) → int
Returns number of the day in year.
getDayOfYear(DateTime dateTime) → int
get day of year. 在今年的第几天.
getDayOfYearByMs(int ms, {bool isUtc = false}) → int
get day of year. 在今年的第几天.
getDaysDifference(DateTime a, DateTime b) → int
Returns count of days between two dates.
getDaysInMonth(int year, int monthNum) → int
Returns number of days in the month of the year.
getDaysInYear(int year) → int
Returns the number of days in a given year.
getLastWeekNumber(int year, {int firstWeekday = DateTime.monday}) → int
Returns number of the last week in year.
getNowDateMs() → int
get Now Date Milliseconds. 获取当前毫秒值
getNowDateStr() → String
get Now Date Str.(yyyy-MM-dd HH:mm:ss) 获取现在日期字符串,默认是:yyyy-MM-dd HH:mm:ss
getNowDateTimeFormat(String outFormat) → String
获取当前日期,返回指定格式
getNowUtcDateTime() → DateTime
获取当前日期返回DateTime(utc)
getUtcDateTimeFormat(String outFormat) → String
获取当前日期,返回指定格式
getWeekday(DateTime? dateTime, {String languageCode = 'en', bool short = false}) → String
get WeekDay. dateTime isUtc languageCode zh or en short
getWeekdayByMs(int milliseconds, {bool isUtc = false, String languageCode = 'en', bool short = false}) → String
get WeekDay By Milliseconds. 获取毫秒值对应是星期几
getWeekNumber(DateTime date, {int firstWeekday = DateTime.monday}) → int
Returns week number in year.
getYesterday({DateTime? now}) → DateTime
获取昨天日期返回DateTime。
getZodiac(DateTime dateTime) → String
星座:按 dateTime 的月日判定(公历)。
getZodiacByMonthDay(int month, int day) → String
星座:按公历 month 月 day 日判定。
isAm(DateTime dateTime) → bool
是否上午(小时 < 12;12 点整起算下午)。
isCurrentDate(DateTime date) → bool
Проверяет является ли заданная дата текущей.
isFirstDayOfMonth(DateTime day) → bool
Checks if day is in the first day of a month.
isFirstDayOfWeek(DateTime day, {int firstWeekday = DateTime.monday}) → bool
Checks if day is in the first day of a week.
isLastDayOfMonth(DateTime day) → bool
Checks if day is in the last day of a month.
isLastDayOfWeek(DateTime day, {int firstWeekday = DateTime.monday}) → bool
Checks if day is in the last day of a week.
isLeapYear(DateTime dateTime) → bool
Return whether it is leap year. 是否是闰年
isLeapYearByMilliseconds(int milliseconds) → bool
Return whether it is leap year. 是否是闰年
isLeapYearByYear(int year) → bool
Return whether it is leap year. 是否是闰年
isPm(DateTime dateTime) → bool
是否下午(小时 >= 12;与 isAm 互补,即 !isAm(dateTime))。
isSameDay(DateTime a, DateTime b) → bool
判断a和b两个时间是否是同一天
isToday(int? milliseconds, {bool isUtc = false, int? locMs}) → bool
is today. 是否是当天.
isWeek(int? ms, {bool isUtc = false, int? locMs}) → bool
is Week. 是否是本周.
isWeekInYear(DateTime date, int year, int firstWeekday) → bool
Checks if week, that contains date is in year.
isYesterday(DateTime dateTime, DateTime locDateTime) → bool
is yesterday by dateTime. 是否是昨天.
isYesterdayByMs(int ms, int locMs, {bool isUtc = false}) → bool
is yesterday by millis. 是否是昨天.
lastDayOfMonth(DateTime dateTime) → DateTime
Returns DateTime that represents a beginning of the last day of the month containing date.
lastDayOfWeek(DateTime dateTime, {int firstWeekday = DateTime.monday}) → DateTime
Returns start of the last day of the week for specified dateTime.
lastDayOfYear(DateTime dateTime) → DateTime
Returns DateTime that represents a beginning of the last day of the year containing date.
nextDay(DateTime d) → DateTime
Returns same time in the next day.
nextMonth(DateTime date) → DateTime
Returns same date in the next month as a DateTime.
nextYear(DateTime d) → DateTime
Returns same date in the next year.
previousDay(DateTime d) → DateTime
Returns same time in the previous day.
previousYear(DateTime d) → DateTime
Returns same date in the previous year.
setTime(DateTime date, int hours, int minutes, [int seconds = 0, int milliseconds = 0, int microseconds = 0]) → DateTime
Creates a copy of date but with time replaced with the new values.
startOfDay(DateTime dateTime) → DateTime
Returns DateTime for the beginning of the day (00:00:00).
startOfNextDay(DateTime dateTime) → DateTime
Returns DateTime for the beginning of the next day (00:00:00).
startOfToday() → DateTime
Returns DateTime for the beginning of today (00:00:00).
yearIsEqual(DateTime dateTime, DateTime locDateTime) → bool
year is equal. 是否同年.
yearIsEqualByMs(int ms, int locMs, {bool isUtc = false}) → bool
year is equal. 是否同年.