Mislav Marohnic' wrote:
> ...
> Ken, these are nice and certainly useful, but I don't think most of
> the people have a need for them. Most of the heavy-duty date/time
> logic should be kept in your application (server-side). But I'll let
> other core members speak for themselves.
>
> You're encouraged to keep maintaining this extensions and releasing
> them open-source so people who do need them can benefit.
> ...
I understand and do plan to release a full-featured version as a
plugin. However, I'd like to submit the following idea for adding 47
lines to core. Again, I understand that it is probably outside of core
functionality--it is unusual that an app needs date fields or date
calculation on every page. I'm just thinking that Prototype has no date
functionality and if ever desired, this is a creative direction.
The snippet below combines some date and date prototype methods along
with Nicolas's suggested number prototype methods. I also have a short
strftime-like format function that I love, but it is short as in 50 more
lines. I'll submit this lean version as a patch once tested.
- Ken
(function() {
var multipliers = {
year: 365.25 * 24 * 60 * 60 * 1000,
month: 30 * 24 * 60 * 60 * 1000,
week: 7 * 24 * 60 * 60 * 1000,
day: 24 * 60 * 60 * 1000,
hour: 60 * 60 * 1000,
minute: 60 * 1000,
second: 1000,
millisecond: 1
};
for (var unit in multipliers) {
multipliers[unit + 's'] = multipliers[unit];
}
for (unit in multipliers) {
Number.prototype[unit] = function() {
return this * multipliers[unit];
};
}
Object.extend(Date.prototype, {
succ: function() {
return new Date(this.getTime() + (24 * 60 * 60 * 1000));
},
add: function(number, unit) {
this.setTime(this.getTime() + (number * multipliers[unit || 'day']));
return this;
},
diff: function(dateObj, unit, allowDecimal) {
dateObj = $D(dateObj);
if (dateObj === null) return null;
var ms = this.getTime() - dateObj.getTime();
var unitDiff = ms / multipliers[unit || 'day'];
return (allowDecimal ? unitDiff : Math.floor(unitDiff));
},
toJSON: function() {
return '"' + this.getFullYear() + '-' +
(this.getMonth() + 1).toPaddedString(2) + '-' +
this.getDate().toPaddedString(2) + 'T' +
this.getHours().toPaddedString(2) + ':' +
this.getMinutes().toPaddedString(2) + ':' +
this.getSeconds().toPaddedString(2) + '"';
}
});
Object.extend(Date, {
create: function(str) {
if (str.constructor === Date) return str;
var ms = Date.parse(str.replace('-', '/'));
return isNaN(ms) ? null : new Date(ms);
}
});
})();
$D = Date.create;
String.prototype.toDate = function() {
return $D(this);
};
// example usage
// $D() is like $(): it can accept a string or a Date object yet always
returns a Date object
$D('08/23/2007 00:00:00');
// Date.prototype.add() [chainable]
$D('August 23, 2007').add(5.months());
// Date.prototype.succ()
$R($D('Aug 23 2007'), $D('Aug 25 2007')).invoke('getTime');
// Date.prototype.diff()
$D('23 Aug 2007').diff('08-25-2007', 'hours'); // 48
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Prototype: Core" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/prototype-core?hl=en
-~----------~----~----~----~------~----~------~--~---