API Documentation - abritinthebay/datejs GitHub Wiki

Static Methods

Static methods do not require a specific date object instance (i.e. var x = new Date();) to be used - they work with the built-in Date object as part of its prototype.

today

Gets a date that is set to the current date. The time is set to the start of the day (00:00 or 12:00 AM).

Syntax
Date.today();
Parameters

None

Return Value

Date Object - The current date.

compare

Compares the first date to the second date and returns an number indication of their relative values. -1 = this is less than date. 0 = values are equal. 1 = this is greater than date.

Syntax
Date.compare( date1, date2 );
Parameters
  • date1 First Date object to compare. required
  • date2 Second Date object to compare to. required
Return Value
  • -1 date1 is less than date2.
  • 0 values are equal.
  • 1 date1 is greater than date2.
Example
	var today = Date.today();
	var past = Date.today().add(-6).days();
	var future = Date.today().add(6).days();

	Date.compare(today, future);                 // -1
	Date.compare(today, new Date().clearTime()); // 0
	Date.compare(today, past);                   // 1
See Also

equals

Compares the first Date object to the second Date object and returns true if they are equal.

Syntax
Date.equals( date1, date2 );
Parameters
  • date1 Date. First Date object to compare. required
  • date2 Date. Second Date object to compare to. required
Return Value

Boolean. True if dates are equal, False if they are not equal.

Example
	var d1 = Date.today();
	var d2 = Date.today().addHours(5);
	var d3 = new Date().clearTime();
	var d4 = "foo";

	Date.equals(d1, d2);  // false
	Date.equals(d1, d3);  // true
	Date.equals(d1, d4);  // throws error
See Also

getDayNumberFromName

Gets the day number (0-6) if given a culture-specific string which is a valid full or abbreviated day name.

Syntax
Date.getDayNumberFromName( dayName );
Parameters
  • dayName String. The name of the day (eg. "Monday, "Mon", "tuesday", "tue", "We", "we"), etc.
Return Value

Number. The day number

Example
	Date.getDayNumberFromName('Tuesday'); // 2
	Date.getDayNumberFromName('sat');     // 6
	Date.getDayNumberFromName('SUNDAY');  // 0

getMonthNumberFromName

Gets the month number (0-11) if given a culture-specific string which is a valid full or abbreviated month name.

Syntax
Date.getMonthNumberFromName ( monthName );
Parameters
  • monthName String. The name of the month (eg. "February, "Feb", "october", "oct").
Return Value

Number. The month number.

Example
	Date.getMonthNumberFromName('January');  // 0
	Date.getMonthNumberFromName('feb');      // 1
	Date.getMonthNumberFromName('July');     // 6
	Date.getMonthNumberFromName('DECEMBER'); // 11

isLeapYear

Returns true if the given year is a leap year, false otherwise.

Syntax
Date.isLeapYear( year )
Parameters
  • year Number. The year.
Return Value

Boolean. True if date is within a LeapYear; otherwise false.

Example
	var date1 = new Date(2008,1,29);
	var date2 = new Date(2007,1,28);
	Date.isLeapYear(date1.getFullYear); // true
	Date.isLeapYear(date2.getFullYear); // false

getDaysInMonth

Date.getDaysInMonth ( Number year, Number month ) : NumberGets the number of days in the month, given a year and month value. Automatically corrects for leap year.

Parameters

  • {Number year} The year.* {Number month} The month (0-11).

Return Value{Number} The number of days in the month.

Example

Date.getDaysInMonth(2008, 1);  // 29 Date.getDaysInMonth(2007, 10); // 30 .

getTimezoneAbbreviation

Date.getTimezoneAbbreviation ( Number timezoneOffset, Boolean isDayLightSavingsTime ) : String[](#Date.getTimezoneAbbreviation_(_Number_timezoneOffset,_Boolean_is)Returns a culture-specific timezone abbreviation based on a given offset and a boolean indicating whether daylight savings time is in effect.

Parameters

  • {Number timezoneOffset} The year.* {Boolean isDayLightSavingsTime} Whether the date instance is observing Daylight Saving Time (Summer Time)

Return Value{String} The timezone abbreviation#### Example

var today = Date.today(); Date.getTimezoneAbbreviation(today.getTimezoneOffset, today.isDayLightSavingsTime()); // "UTC", "GMT", "EST", "PDT", etc. .

getTimezoneOffset

Date.getTimezoneOffset ( String timezoneAbbreviation, Boolean isDayLightSavingsTime ) : Number[](#Date.getTimezoneOffset_(_String_timezoneAbbreviation,_Boolean_is)Gets the timezone offset if given a culture-specific string which is a valid full or abbreviated timezone name and a boolean indicating whether daylight savings time is in effect.#### Parameters

  • {Number timezoneAbbreviation} The timezone abbreviation* {Boolean isDayLightSavingsTime} Whether the date instance is observing Daylight Saving Time (Summer Time)

Return Value{String} The timezone abbreviation

Example

Date.getTimezoneOffset("PST", true); .

parse

Date.parse ( String dateString ) : DateConverts the specified string value into its JavaScript Date equivalent using culture-specific format information.

Parameters

  • {String dateString} The string value to convert into a Date object. required#### Return Value{Date} A Date object or null if the string cannot be converted into a Date.

Example

Date.parse("today") Date.parse("t + 5 d") // today + 5 days Date.parse("next thursday") Date.parse("February 20th 1973?) Date.parse("Thu, 1 July 2004 22:30:00?) .

parseExact

Date.parseExact ( String dateString, String formatStringOrArray ) : Date[](#Date.parseExact_(_String_dateString,_String_formatStringOrArray)Converts the specified string value into its JavaScript Date equivalent using the specified format (string) or formats (array). The format of the string value must match one of the supplied formats exactly.

Parameters

  • {String dateString} The string value to convert into a Date object. required.* {Object formatStringOrArray} The expected format {String} or an array of expected formats {Array} of the date string. required.

Return Value{Date} A Date object or null if the string cannot be converted into a Date.

Example

Date.parseExact("10/15/2004", "M/d/yyyy");  // The Date of 15-Oct-2004 Date.parse("15-Oct-2004", "d-MMM-yyyy");    // The Date of 15-Oct-2004 Date.parse("2004.10.15", "yyyy.MM.dd");     // The Date of 15-Oct-2004 Date.parseExact("10/15/2004", ["M/d/yyyy", "MMMM d, yyyy"]); // The Date of 15-Oct-2004 .

validateDay

Date.validateDay ( Number day, Number fullYear, Number monthNumber ) : Boolean[](#Date.validateDay_(_Number_day,_Number_fullYear,_Number_monthNumb)Validates the number is within an acceptable range for the days in a month [0-MaxDaysInMonth].

Parameters

  • {Number day} The number to check if within range.* {Number fullYear} The number to check if within range.* {Number monthNumber} The number to check if within range.

Return Value{Boolean} true if within range, otherwise false.

Example

Date.validateDay(15, 2007, 1);  // true, 15-Feb-2007 Date.validateDay(31, 2007, 10); // false, throws RangeError exception .

validateHour

Date.validateHour ( Number hour ) : BooleanValidates the number is within an acceptable range for hours [0-23]. Returns true if within range, otherwise false.

Parameters

  • {Number hour} The number to check if within range.

Return Value{Boolean} true if within range, otherwise false.

Example

Date.validateHour(15); // true Date.validateHour(24); // false, throws RangeError exception .

validateMillisecond

Date.validateMillisecond ( Number milliseconds ) : BooleanValidates the number is within an acceptable range for milliseconds [0-999]. Returns true if within range, otherwise false.

Parameters

  • {Number milliseconds} The number to check if within range.

Return Value{Boolean} true if within range, otherwise false.

Example

Date.validateMillisecond(500); // true .

validateMinute

Date.validateMinute ( Number minutes ) : BooleanValidates the number is within an acceptable range for minutes [0-59]. Returns true if within range, otherwise false.

Parameters

  • {Number minutes} The number to check if within range. #### Return Value{Boolean} true if within range, otherwise false.

Example

Date.validateMinute(45); // true Date.validateMinute(60); // false, throws RangeError exception .

validateMonth

Date.validateMonth ( Number month ) : BooleanValidates the number is within an acceptable range for months [0-11].

Parameters

  • {Number month} The number to check if within range.

Return Value{Boolean} true if within range, otherwise false.

Example

Date.validateMonth(0);  // true, "January" Date.validateMonth(12); // false, throws RangeError exception .

validateSecond

Date.validateSecond ( Number second ) : BooleanValidates the number is within an acceptable range for seconds [0-59]. Returns true if within range, otherwise false.

Parameters

  • {Number second} The number to check if within range. #### Return Value{Boolean} true if within range, otherwise false.

Example

Date.validateSecond(15); // true Date.validateSecond(60); // false, throws RangeError exception .

validateYear

Date.validateYear ( Number year ) : BooleanValidates the number is within an acceptable range for years [0-9999].

Parameters

  • {Number year} The number to check if within range.

Return Value{Boolean} true if within range, otherwise false.

Example

Date.validateYear(2007); // true