New .same() Function, But Is It Useful?

May 13th, 2008 | Code

I need some feedback regarding a feature I just added to the library, but I'm struggling to explain/document clearly and I'm not sure if others would find this useful.

The new .same() function will compare two date objects to determine if they occur on/in exactly the same instance of the given date part. (...like I said, I'm having a hard time explaining this one).

It's easier to understand with code...

Scenario: Determine if the two dates occur on the same day.

Example

var d1 = Date.today(); // today at 00:00
var d2 = new Date();   // exactly now.

// Do they occur on the same day?
d1.same().day(d2); // true

// What if it's the same day, but one year apart?
var nextYear = Date.today().add(1).year();

d1.same().day(nextYear); // false, because the dates must occur on the exact same day.

When comparing the dates in the same above, the .same() function will test that the year, month, and day match. Because we're only interested in whether the day is the same, the hour, minute, second & millisecond values are ignored.

Using the same d1 & d2 values from above, determine if the two dates occur on the same hour.

Example

// Do they occur on the same hour?
d1.same().hour(d2); // false, unless d2 hour is '00' (< 1:00 am).

If no date is passed as a parameter, by default the .same() function will use 'Now' (new Date()) as the default date to compare against.

Example

Date.today().same().day(); // true, 'today' occurs on 'today'
Date.today().same().hour(); // false, unless 'now' is '00' minutes (< 1:00am)

What do think? Would you find this functionality useful?

I do... because I'm often trying to determine if some date object occurs during today. The following samples demonstrate various ways to determine if a date object occurs today.

Example (native JavaScript, without Datejs)

var temp = new Date();
temp.setHours(0);
temp.setMinutes(0);
temp.setSeconds(0);
temp.setMilliseconds(0);

var today = new Date(someDate.getTime()); // clone original instance so we don't actually change the original date.
var today.setHours(0);
var today.setMinutes(0);
var today.setSeconds(0);
var today.setMilliseconds(0);

(today == temp); // true|false

Example (with Datejs)

// with Datejs, but without .same
someDate.clone().clearTime().equals(Date.today());

// New
someDate.same().day();

// Or,
someDate.is().today();

Checking if a given date object occurs today is easy enough with Datejs and would not really justify adding the new .same() functionality, but things can get nasty when you need to compare against a date other than 'today'.

Scenario: Determine if a given date occurs during some week period 2 months from now.

Example

// New
var future = Date.today().add(2).months();

return someDate.same().week(future); // true|false;

The .same() function adds a third context to .second() within the library, which enables the following interesting (but freakishly cryptic) logic.

Example

Date.jan().second().monday().add(1).second().same().second();

The .same() functionality has been added to sugarpak.js and is available from SVN.

Any feedback is appreciated.

7 Comments

  1. [...] geoffrey.mcgill wrote an interesting post today on New .same() function, but is it useful?Here’s a quick excerptI need some feedback regarding a feature I just added to the library, but I’m struggling to explain/document clearly and I’m not sure if others would find this useful. The new .same() function will compare two date objects to determine … [...]

    Pingback by Library » Blog Archive » New .same() function, but is it useful? — May 13, 2008 @ 7:13 pm

  2. This looks useful.

    Comment by David — May 14, 2008 @ 6:35 am

  3. I would definitely use it. Great idea!

    Comment by Schalk Neethling — May 15, 2008 @ 6:08 am

  4. Why not call it sameDay() or isSameDay()?

    Comment by Mike — August 5, 2009 @ 7:45 am

  5. Seems to be very useful to me.

    @Mike, because its not only used for days. You can also use d1.same().month(d2) or d1.same.hour(d2) for example, to check whether two event are in the same month (of the same year) or the same hour (of the same day of the same month of the same year) respectively.

    Comment by JW — August 23, 2009 @ 6:54 am

  6. It looks like you’re extending the Number class too. It looked nice till then.

    Comment by Rob Colburn — September 4, 2009 @ 10:56 am

  7. Hi,

    It looks like the contact form on your contact page is broken, so I’m posting here instead in the hope that you will see this comment.

    Are you still maintaining / doing any sort of active development on this code? I’m asking because it’s really quite awesome and I’d like to use it in a Thunderbird add-in I maintain, but there’s one little feature missing… I’d like to be able to say, for example, “8:30 am next weekday” and have “weekday” mean the next day if it is currently Monday through Thursday, or the following Monday if it is currently Saturday or Sunday.

    I looked briefly at the code to see if this would be something easy to add, and boy, it’s complicated!

    Comment by Jonathan Kamens — July 17, 2010 @ 5:30 pm

Leave a comment