Opened 10 years ago

Closed 3 years ago

#9021 closed enhancement (patchwelcome)

Date Utils: Truncate Date

Reported by: Jarrod Carlson Owned by: Adam Peller
Priority: high Milestone: 1.13
Component: Dojox Version:
Keywords: date utils Cc: Nathan Toone, mde
Blocked By: Blocking:

Description

Attached is a new module that I started to provide some additional Date utility functionality that I have not found in Dojo. I'm sure this can expand, but I'm using it to allow a Date object to be truncated, similar to what is provided by the Commons Lang DateUtils? in Java (http://commons.apache.org/lang/api-release/org/apache/commons/lang/time/DateUtils.html)

Though the contract differs somewhat from Commons Lang, I found good use in being able to selectively truncate a Date's parts.

To truncate a a Date, do:

  var t = dojox.date.utils.truncate(date, dojo.date.HOUR+dojo.date.MINUTE+dojo.date.SECOND+dojo.date.MILLISECOND);

The truncation is performs using a bitmask for the fields to truncate.

Comments?

Attachments (1)

utils.js (1.5 KB) - added by Jarrod Carlson 10 years ago.
dojox.date.utils

Download all attachments as: .zip

Change History (9)

Changed 10 years ago by Jarrod Carlson

Attachment: utils.js added

dojox.date.utils

comment:1 Changed 10 years ago by dante

Cc: Adam Peller added

@peller, you are the resident date expert ... comments?

comment:2 Changed 10 years ago by Jarrod Carlson

I actually discovered a slight bug in this code which I have yet to come up with a good solution.

I'm not sure if I would consider it a bug or "by design" but it is worth noting.

If you have a date of Feb. 29 in a leap year and you try to truncate the year (1970 is not a leap year), The date will roll over to March 1st in that case.

Perhaps the best way to address this would be to truncate the date object to the precision you specify, and no less. So do not allow truncation of the hour without also truncating the minute, second and millisecond.

Thoughts?

comment:3 Changed 10 years ago by Adam Peller

Milestone: tbd1.5

I'm a little leery of anything involving native Date objects at this point :) We could commit this as experimental to dojox.date and work on it there, if you'd be willing to maintain it. One thing that jumps out -- can we use strings instead of enum style args, like dojo.date does?

I think I encountered something similar to your leap month problem in dojo.date and we used Jan 1 1972 for our computations, or something like that.

comment:4 Changed 10 years ago by Adam Peller

Cc: Nathan Toone added; Adam Peller removed
Component: CoreDojox
Owner: changed from anonymous to Adam Peller

comment:5 Changed 10 years ago by Adam Peller

Cc: mde added

comment:6 Changed 9 years ago by Adam Peller

Milestone: 1.51.6

comment:7 Changed 9 years ago by Adam Peller

Milestone: 1.6future

comment:8 Changed 3 years ago by dylan

Milestone: future1.12
Resolution: patchwelcome
Status: newclosed

Given that no one has shown interest in creating a patch in the past 5+ years, I'm closing this as patchwelcome.

Note: See TracTickets for help on using tickets.