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 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
Adds days to current date.
dquery("2011-09-09").addDays(5);
Adds hours to current date.
dquery("2011-09-09").addHours(10);
Adds milliseconds to current date.
dquery("2011-09-09").addMilliseconds(20);
Adds minutes to current date.
dquery("2011-09-09").addMinutes(50);
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.
dquery("2011-09-09").addMonths(20)
Adds seconds to current date.
dquery("2011-09-09").addSeconds(20);
Adds weeks to current date.
dquery("2011-09-09").addWeeks(4);
Adds years to current date.
dquery("2011-09-09").addYears(9);
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.
var foo = dquery(); var bar = foo.clone();
Returns the amount of days of the current date's month.
var daysInSeptember = dquery("2011-09-01").daysInMonth();
Returns a date list of all the days of the current month.
var days = dquery().daysOfMonth();
days.each(function(date) {
alert(date);
});
Returns a date list of all the days of the current date's week.
var days = dquery().daysOfWeek();
days.each(function(date) {
alert(date);
});
Changes the day to the first day of the current date's month.
var foo = dquery().firstDayOfMonth();
Changes the day to the first day of the current date's year.
var foo = dquery().firstDayOfYear();
Changes to the first week of the year.
var bar = dquery().firstWeek();
Formats the current date according to the rules specified by the formatString.
| HH | Hour (00-23) |
| H | Hour (0-23) |
| h | Hour (0-12) |
| hh | Hour (00-12) |
| M | Minute (0-59) |
| MM | Minute (00-59) |
| s | Second (0-59) |
| ss | Second (00-59) |
| yy | Year (00-99) |
| yyyy | Year (0000-9999) |
| m | Month (1-12) |
| mm | Month (01-12) |
| mmm | Month (Jan-Feb) |
| mmmm | Month (January-February) |
| dddd | Day (Monday-Sunday) |
| ddd | Day (Mon-Sun) |
| dd | Day (01-31) |
| d | Day (1-31) |
| a | AM/PM |
dquery().format("yyyy-mm-dd");
dquery().format("d mmm yyyy, hh:MMa");
Gets the current week number. Depends on weekstart configuration.
var currentWeek = dquery().getWeek();
Returns whether year is a leap year.
var isLeapYear = dquery().isLeapYear();
Returns whether day is tomorrow.
var date = dquery("some date");
if (date.isTomorrow()) {
alert("Tomorrow!");
}
Returns whether day is yesterday.
var date = dquery("some date");
if (date.isYesterday()) {
alert("Yesterday!");
}
Sets date to last day of current date's month.
var foo = dquery().lastDayOfMonth();
Sets date to the last day of the current date's year.
var foo = dquery().lastDayOfYear();
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.
|
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.
|
Resets date to 1970-01-01
Resets time to 00:00:00.0
Compares to date. Returns true if year, month and date is the same.
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.
|
Sets week to week.
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 tzmm/dd/yymm/dd/yy HH:MMmm/dd/yy HH:MM:ssyyyy-mmyy-mm-ddyymmddyy-mm-dd HH:MMyy-mm-dd HH:MM:ssyy-mm-ddTHH:MM:sstzyy-mm-ddTHH:MM:ssZSee format for more information. |
date
|
An instance of Date |
Does the same as dquery.iterate, except that it returns a list of all the iterated values.
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.
|
Array of language representation of am/pm. Default is ["am", "pm"].
Array of language representations of weekdays. Default is ["Sun", "Mon", Tue", "Wed", "Thu", "Fri", "Sat", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"].
String of which day a week starts from. Default is "monday".
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 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.
Calls a callback function on each date in the list.
dquery().daysInWeek().each(function(date) {
alert(date);
});
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.
var formattedDays = dquery().daysInWeek().map(function(date) {
return date.format("yyyy-mm-dd");
});