View source

class thx.Dates

Available on all platforms

Dates provides additional extension methods on top of the Date type.

using Dates;

@author Jason O'Neil @author Franco Ponticelli

Class Fields

static function compare(a:Date, b:Date):Int

It compares two dates.

static function create(year:Int, month:Int = 0, day:Int = 1, hour:Int = 0, minute:Int = 0, second:Int = 0):Date

Creates a Date by using the passed year, month, day, hour, minute, second.

Note that each argument can overflow its normal boundaries (e.g. a month value of -33 is perfectly valid) and the method will normalize that value by offsetting the other arguments by the right amount.

static function daysInMonth(year:Int, month:Int):Int

Returns the number of days in a month.

Parameters:
month

An integer representing the month. (Jan=0, Dec=11)

year

An 4 digit integer representing the year.

Returns:

Int, the number of days in the month.

Throws:
Error

if the month is not between 0 and 11.

static function daysInThisMonth(d:Date):Int

Tells how many days in the month of the given date.

Parameters:
date

The date representing the month we are checking.

Returns:

Int, the number of days in the month.

static function daysRange(start:Date, end:Date):Array<Date>

Creates an array of dates that begin at start and end at end included.

Time values are pick from the start value except for the last value that will match end. No interpolation is made.

static inline function equals(self:Date, other:Date):Bool

Returns true if the passed dates are the same.

static inline function greater(self:Date, other:Date):Bool

Returns true if the self date is greater than other.

static inline function greaterEquals(self:Date, other:Date):Bool

Returns true if the self date is greater than or equal to other.

static inline function isInLeapYear(d:Date):Bool

Tells if the given date is inside a leap year.

Parameters:
date

The date object to check.

Returns:

True if it is in a leap year, false otherwise.

static function isLeapYear(year:Int):Bool

Tells if a year is a leap year.

Parameters:
year

The year, represented as a 4 digit integer

Returns:

True if a leap year, false otherwise.

static function jump(date:Date, period:TimePeriod, amount:Int):Date

Get a date relative to the current date, shifting by a set period of time.

Please note this works by constructing a new date object, rather than using DateTools.delta(). The key difference is that this allows us to jump over a period that may not be a set number of seconds. For example, jumping between months (which have different numbers of days), leap years, leap seconds, daylight savings time changes etc.

@param date The starting date. @param period The TimePeriod you wish to jump by, Second, Minute, Hour, Day, Week, Month or Year. @param amount The multiple of period that you wish to jump by. A positive amount moves forward in time, a negative amount moves backward.

static inline function less(self:Date, other:Date):Bool

Returns true if the self date is lesser than other.

static inline function lessEqual(self:Date, other:Date):Bool

static inline function lessEquals(self:Date, other:Date):Bool

Returns true if the self date is lesser than or equal to other.

static function max(self:Date, other:Date):Date

Finds and returns which of the two passed dates is the newest.

static function min(self:Date, other:Date):Date

Finds and returns which of the two passed dates is the oldest.

static inline function more(self:Date, other:Date):Bool

static inline function moreEqual(self:Date, other:Date):Bool

static function nearEquals(self:Date, other:Date, units:Int = 1, ?period:TimePeriod):Bool

Returns true if the dates are approximately equals. The amount of delta allowed is determined by units and it spans that amount equally before and after the self date. The default unit value is 1.

The default period range is Second.

static inline function nextDay(d:Date):Date

Returns a new date, exactly 1 day after the given date/time.

static inline function nextHour(d:Date):Date

Returns a new date, exactly 1 hour after the given date/time.

static inline function nextMinute(d:Date):Date

Returns a new date, exactly 1 minute after the given date/time.

static inline function nextMonth(d:Date):Date

Returns a new date, exactly 1 month after the given date/time.

static inline function nextSecond(d:Date):Date

Returns a new date, exactly 1 second after the given date/time.

static inline function nextWeek(d:Date):Date

Returns a new date, exactly 1 week after the given date/time.

static inline function nextYear(d:Date):Date

Returns a new date, exactly 1 year after the given date/time.

static function numDaysInMonth(month:Int, year:Int):Int

static function numDaysInThisMonth(d:Date):Int

static inline function prevDay(d:Date):Date

Returns a new date, exactly 1 day before the given date/time.

static inline function prevHour(d:Date):Date

Returns a new date, exactly 1 hour before the given date/time.

static inline function prevMinute(d:Date):Date

Returns a new date, exactly 1 minute before the given date/time.

static inline function prevMonth(d:Date):Date

Returns a new date, exactly 1 month before the given date/time.

static inline function prevSecond(d:Date):Date

Returns a new date, exactly 1 second before the given date/time.

static inline function prevWeek(d:Date):Date

Returns a new date, exactly 1 week before the given date/time.

static inline function prevYear(d:Date):Date

Returns a new date, exactly 1 year before the given date/time.

static function sameDay(self:Date, other:Date):Bool

Returns true if the 2 dates share the same year, month and day.

static function sameHour(self:Date, other:Date):Bool

Returns true if the 2 dates share the same year, month, day and hour.

static function sameMinute(self:Date, other:Date):Bool

Returns true if the 2 dates share the same year, month, day, hour and minute.

static function sameMonth(self:Date, other:Date):Bool

Returns true if the 2 dates share the same year and month.

static function sameYear(self:Date, other:Date):Bool

Returns true if the 2 dates share the same year.

static inline function snapNext(date:Date, period:TimePeriod):Date

Snaps a Date to the next second, minute, hour, day, week, month or year.

Parameters:
date

The date to snap. See Date.

period

Either: Second, Minute, Hour, Day, Week, Month or Year

Returns:

The snapped date.

static function snapNextWeekDay(date:Date, day:Weekday):Date

Snaps a date to the next given weekday. The time within the day will stay the same.

If you are already on the given day, the date will not change.

Parameters:
date

The date value to snap

day

Day to snap to. Either Sunday, Monday, Tuesday etc.

Returns:

The date of the day you have snapped to.

static inline function snapPrev(date:Date, period:TimePeriod):Date

Snaps a Date to the previous second, minute, hour, day, week, month or year.

Parameters:
date

The date to snap. See Date.

period

Either: Second, Minute, Hour, Day, Week, Month or Year

Returns:

The snapped date.

static function snapPrevWeekDay(date:Date, day:Weekday):Date

Snaps a date to the previous given weekday. The time within the day will stay the same.

If you are already on the given day, the date will not change.

Parameters:
date

The date value to snap

day

Day to snap to. Either Sunday, Monday, Tuesday etc.

Returns:

The date of the day you have snapped to.

static inline function snapTo(date:Date, period:TimePeriod):Date

Snaps a Date to the nearest second, minute, hour, day, week, month or year.

Parameters:
date

The date to snap. See Date.

period

Either: Second, Minute, Hour, Day, Week, Month or Year

Returns:

The snapped date.

static function snapToWeekDay(date:Date, day:Weekday, firstDayOfWk:Weekday = Sunday):Date

Snaps a date to the given weekday inside the current week. The time within the day will stay the same.

If you are already on the given day, the date will not change.

Parameters:
date

The date value to snap

day

Day to snap to. Either Sunday, Monday, Tuesday etc.

firstDayOfWk

The first day of the week. Default to Sunday.

Returns:

The date of the day you have snapped to.

static function withDay(date:Date, day:Int):Date

Returns a new date that is modified only by the day.

static function withHour(date:Date, hour:Int):Date

Returns a new date that is modified only by the hour.

static function withMinute(date:Date, minute:Int):Date

Returns a new date that is modified only by the minute.

static function withMonth(date:Date, month:Int):Date

Returns a new date that is modified only by the month (remember that month indexes begin at zero).

static function withSecond(date:Date, second:Int):Date

Returns a new date that is modified only by the second.

static function withYear(date:Date, year:Int):Date

Returns a new date that is modified only by the year.

Toggle inherited fields