Skip to content

torholm/dquery

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

49 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

dquery

Introduction

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.

See documentation for more information on how to use dquery.

Features

Unobtrusive

dquery doesn't pollute anything, except that it adds an object to window (dquery of course). A warning, though: if you use libraries that break Date's normal behaviour, dquery will probably not work properly.

Chaining

Most methods on dquery objects are chained, i.e. they return the object that was modified.

Example:

dquery().addDays(4).set({ hours: 20 });

Some of the stuff you can do

Deal with a normal Date object:

var date = new Date();
/* ... */
var yay = dquery(date);

Is the date in the dquery object in the future?

var something = dquery();
/* ... */
if (something > new Date) {
    dqueryIsTheFuture();
}

Add 4 years:

dquery("5/21/2011").addYears(4).toString()
// Thu May 21 2015 00:00:00 GMT+0200 (CEST)

Reset time to 00:00:00:

dquery().resetTime().toString()
// Sat May 21 2011 00:00:00 GMT+0200 (CEST)

Iterate all days of this month:

dquery.iterate( 
    "days", 
    dquery().firstDayOfMonth(),
    dquery().lastDayOfMonth(),
function( date ) {
    /* date parameter above is a dquery object */
});

Format date and time:

dquery("5/12/2011").format("yyyy-mm-dd");
// 2011-05-12
dquery("5/12/2011 14:50").format("m/d/yy hh:MM");
// 5/12/11 14:50
dquery("5/12/2011 00:00").format("ha");
// 12am
dquery("5/12/2011 12:00").format("ha");
// 12pm