Fork me on GitHub

dquery is a useful tool for handling dates and times. It works by extending instances of Date with additional functionality. Unfortunately, you can't just simply copy Date.prototype, like jQuery does with Array.prototype. So, we lose a bit of performance when we call dquery(). The method simply creates a new Date object, and then sticks a bunch of methods onto it. The end result is a Date object that you can use just like an ordinary Date instance, but with more functionality.

dquery documentation

dquery object functions:
addDays addHours addMilliseconds addMinutes addMonths addSeconds addWeeks addYears clone daysInMonth daysOfMonth daysOfWeek firstDayOfMonth firstDayOfYear firstWeek format getWeek isLeapYear isTomorrow isYesterday lastDayOfMonth lastDayOfYear next prev resetDate resetTime sameDate set setWeek

dquery core:
dquery dquery.collect dquery.iterate

i8n:
dquery.i8n.ampm dquery.i8n.weekdays dquery.i8n.weekstart dquery.i8n.months

dquery.DateList object:
dquery.DateList each map

dquery object functions

addDays( days )

Adds days to current date.

Example

dquery("2011-09-09").addDays(5);

addHours( hours )

Adds hours to current date.

Example

dquery("2011-09-09").addHours(10);

addMilliseconds( milliSecs )

Adds milliseconds to current date.

Example

dquery("2011-09-09").addMilliseconds(20);

addMinutes( minutes )

Adds minutes to current date.

Example

dquery("2011-09-09").addMinutes(50);

addMonths( months )

Adds months to current date.

Note! If you change to a month that have fewer days than the current date, the date will be the last day of the new month.

Example

dquery("2011-09-09").addMonths(20)

addSeconds( seconds )

Adds seconds to current date.

Example

dquery("2011-09-09").addSeconds(20);

addWeeks( weeks )

Adds weeks to current date.

Example

dquery("2011-09-09").addWeeks(4);

addYears( years )

Adds years to current date.

Example

dquery("2011-09-09").addYears(9);

clone()

Creates an exact clone -- a new instance -- of current date.

Use this, for example, to get a throw-away instance that you can mess around with without modifying the original date.

Example

var foo = dquery();
var bar = foo.clone();

daysInMonth()

Returns the amount of days of the current date's month.

Example

var daysInSeptember = dquery("2011-09-01").daysInMonth();

daysOfMonth()

Returns a date list of all the days of the current month.

Example

var days = dquery().daysOfMonth();
days.each(function(date) { 
  alert(date);
});

daysOfWeek()

Returns a date list of all the days of the current date's week.

Example

var days = dquery().daysOfWeek();
days.each(function(date) {
  alert(date);
});

firstDayOfMonth()

Changes the day to the first day of the current date's month.

Example

var foo = dquery().firstDayOfMonth();

firstDayOfYear()

Changes the day to the first day of the current date's year.

Example

var foo = dquery().firstDayOfYear();

firstWeek()

Changes to the first week of the year.

Example

var bar = dquery().firstWeek();

format( formatString )

Formats the current date according to the rules specified by the formatString.

HHHour (00-23)
HHour (0-23)
hHour (0-12)
hhHour (00-12)
MMinute (0-59)
MMMinute (00-59)
sSecond (0-59)
ssSecond (00-59)
yyYear (00-99)
yyyyYear (0000-9999)
mMonth (1-12)
mmMonth (01-12)
mmmMonth (Jan-Feb)
mmmmMonth (January-February)
ddddDay (Monday-Sunday)
dddDay (Mon-Sun)
ddDay (01-31)
dDay (1-31)
aAM/PM

Example

dquery().format("yyyy-mm-dd");
dquery().format("d mmm yyyy, hh:MMa");

getWeek()

Gets the current week number. Depends on weekstart configuration.

Example

var currentWeek = dquery().getWeek();

isLeapYear()

Returns whether year is a leap year.

Example

var isLeapYear = dquery().isLeapYear();

isTomorrow()

Returns whether day is tomorrow.

Example

var date = dquery("some date"); 
if (date.isTomorrow()) {
  alert("Tomorrow!");
}

isYesterday()

Returns whether day is yesterday.

Example

var date = dquery("some date");
if (date.isYesterday()) {
  alert("Yesterday!");
}

lastDayOfMonth()

Sets date to last day of current date's month.

Example

var foo = dquery().lastDayOfMonth();

lastDayOfYear()

Sets date to the last day of the current date's year.

Example

var foo = dquery().lastDayOfYear();

next( day [, options ] )

Skips to the next day which matches ``day''.

day should be either "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"
options (optional) should be an object. Properties: exceptSame which can be true/false. Default is false. If true, dquery won't do anything if the current date's day is the same as day.

prev( day [, options ] )

Skips to the previous day which matches ``day''.

day should be either "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"
options (optional) should be an object. Properties: exceptSame which can be true/false. Default is false. If true, dquery won't do anything if the current date's day is the same as day.

resetDate()

Resets date to 1970-01-01

resetTime()

Resets time to 00:00:00.0

sameDate( date )

Compares to date. Returns true if year, month and date is the same.

set( unit, value )

set( options )

Sets unit to value.

unit can be "date", "year", "month", "seconds", "minutes", "hours", "ms".
value any numeric value that applies to unit.
options map that contains units/value.
See unit parameter above.

setWeek( week )

Sets week to week.

dquery core

dquery( timestamp )

dquery( dateString )

dquery( date )

dquery()

Constructs a dquery object.

timestamp A millisecond timestamp.
dateString A string representation of a date.
Accepted formats are:
ddd, dd mmm yy} HH:MM:ss tz
mm/dd/yy
mm/dd/yy HH:MM
mm/dd/yy HH:MM:ss
yy
yy-mm
yy-mm-dd
yymmdd
yy-mm-dd HH:MM
yy-mm-dd HH:MM:ss
yy-mm-ddTHH:MM:sstz
yy-mm-ddTHH:MM:ssZ

See format for more information.
date An instance of Date

dquery.collect( options )

Does the same as dquery.iterate, except that it returns a list of all the iterated values.

dquery.iterate( options, callback )

Runs through all dates between a start and a stop date.

start Start date. Can be of type Date, dquery, a valid date string, or a timestamp.
stop Stop date. Can be of type Date, dquery, a valid date string, or a timestamp.
metric What unit to increment. Can be "milliseconds", "seconds", "minutes", "hours", "days", "weeks", "months", "years".
step How much to increment at each step. Default is 1.
filter function( date )
callback will be called if function returns true. date is the current iterated date.

i8n:

dquery.i8n.ampm

Array of language representation of am/pm. Default is ["am", "pm"].

dquery.i8n.weekdays

Array of language representations of weekdays. Default is ["Sun", "Mon", Tue", "Wed", "Thu", "Fri", "Sat", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"].

dquery.i8n.weekstart

String of which day a week starts from. Default is "monday".

dquery.i8n.months

Array of language representations of months. Default is ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "January", "February", "Mars", "April", "May", "June", "July", "August", "September", "October", "November", "December"].

dquery.DateList object

dquery.DateList

dquery.DateList is a class that is supposed to hold a bunch of dates.

It has utility functions, like each and map. Functions like daysOfWeek and daysOfMonth returns a DateList containing the results.

each( callback )

Calls a callback function on each date in the list.

Example

dquery().daysInWeek().each(function(date) {
  alert(date);
});

map( callback )

Calls a callback function on each date in the list, and collects the return values from the callback function into a new list, which is returned.

Example

var formattedDays = dquery().daysInWeek().map(function(date) {
  return date.format("yyyy-mm-dd");
});