JavaScript

DategetParts Method

Syntax

Date.getParts([scope])

Arguments

scopestring

The scope of the parts to return. Values can be "all" (the default), "year", "month", "date" or "time".

Returns

partsobject

The parts that make up the date.

yearobject

The year level data.

valuenumber

The year.

isLeapboolean

True if the year is a leap year.

weeksnumber

The number of weeks in the year.

daysnumber

The number of days in the year.

monthobject

The month level data.

valuenumber

The zero base value of the month.

numbernumber

The one based value of the month.

fullstring

The full name of the month as defined by A5.d.date.months.

shortstring

The short name of the month as defined by A5.d.date.monthsShort.

weeksnumber

The number of weeks in the month.

daysnumber

The number of days in the month.

weekobject

The week level data.

ofYearnumber

The week of the year.

ofMonthnumber

The week of the month.

dayobject

The day level data.

ofYearnumber

The day of the year.

ofMonthnumber

The day of the month.

ofWeekobject

The day of the week.

valuenumber

The zero base value of the weekday starting on Sunday.

fullstring

The full name of the day as defined by A5.d.date.days.

shortstring

The short name of the day as defined by A5.d.date.daysShort.

minimalstring

The minimal name of the day as defined by A5.d.date.daysMinimal.

ofMonthnumber

The instance of the weekday of the month of a the date. For example "2" for the second Tuesday of the month.

timeobject

The time of day.

hoursnumber

A 24 hour value of the hour of the given date.

minutesnumber

The value of the minute of the given date.

secondsnumber

The value of the second of the given date.

millisecondsnumber

The value of the milliseconds of the given date.

Description

Extension to the native date variable to the get some or all of the parts that make up the date.

Discussion

The getParts method of the date variable returns the assorted parts of a date. The parts return can be limited by passing is a scope.

The default scope is "all", which will return all the parts of the date. A value of "year" will only return the year values of a given date. A value of "month" will return the year and month values. A value of "date" will return the year, month, week and day values, but not the time. A value of "time" will return only the time values.

Example

var d = new Date(2018,7,10,12,34);
var p = d.getParts('month');
// p = {
//	"year": {
//		"value": 2018,
//		"isLeap": false,
//		"weeks": 52,
//		"days": 365
//	},
//	"month": {
//		"value": 7,
//		"full": "august",
//		"short": "aug",
//		"number": 8,
//		"weeks": 5,
//		"days": 31
//	}
//}