[Prototype-core] Date prototype functions [PLUGIN]

2007-08-31 Thread Ken Snyder
Mislav Marohnic' wrote:
> On 8/22/07, *Ken Snyder* <[EMAIL PROTECTED] 
> > wrote:
>
> What is the feeling on adding functions to Date.prototype?  As mentioned 
> in March (see message at bottom), adding Date.prototype.succ() would 
> allow some helpful iterating.  I would suggest an optional argument to 
>
> succ for time period and I also would like to throw out several more 
> possible methods (below).
>
>
> 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.
For any interested, here is the final version of my Date plugin:
http://kendsnyder.com/sandbox/date/

I've also added it as a Prototype patch if there is ever a desire to 
extend Date within Prototype
http://dev.rubyonrails.org/ticket/9452

- Ken Snyder


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Prototype: Core" group.
To post to this group, send email to prototype-core@googlegroups.com
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
-~--~~~~--~~--~--~---



[Prototype-core] Date prototype functions [BUMP]

2007-08-23 Thread Ken Snyder
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 prototype-core@googlegroups.com
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
-~--~~~~--~~--~--~---



[Prototype-core] Date prototype functions [BUMP]

2007-08-22 Thread Ken Snyder
What is the feeling on adding functions to Date.prototype?  As mentioned 
in March (see message at bottom), adding Date.prototype.succ() would 
allow some helpful iterating.  I would suggest an optional argument to 
succ for time period and I also would like to throw out several more 
possible methods (below).

- Ken Snyder


// format
var d = new Date().format('%Y-%m-%d'); // 2007-08-17
// create(static)
Date.create('8-17-07').format('%Y-%m-%d'); // 2007-08-17
// add
var d = new Date().add(2, 'month').format('%Y-%m-%d'); // 2007-10-17
// diff
new Date().diff(d, 'month'); // 2
// succ
new Date().succ('year').format('%Y'); // 2008
// isEqual
new Date().isEqual('2007-08-17', 'day'); // true
// isBefore
new Date().isBefore('08-18-2007 7:00pm'); // true
// isAfter
new Date().isAfter('8/16/2007'); // true
// withinRange
new Date().withinRange('8/1/2007', '8-31-07'); // true
// daysInMonth (static)
Date.daysInMonth(2007, 2); // 28
// ISO (static property)
Date.create('8-17-2007').format(Date.ISO); // 2007-08-17 00:00:00


If the team is interested, I could refactor my current implementation 
for these functions and submit it as a patch for close review.  I 
understand users would probably want it lean and light if included at all.





2007-03-10 agrath wrote:

> > ...
> >
> > This is a succ function for the date prototype and could be a worthy
> > addition.
> >
> > Date.prototype.succ = function(){
> >   return new Date(this.getFullYear(), this.getMonth(), this.getDate()
> > +1);
> > }
> >
> > ...
>   

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Prototype: Core" group.
To post to this group, send email to prototype-core@googlegroups.com
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
-~--~~~~--~~--~--~---



[Prototype-core] Date prototype functions

2007-08-17 Thread Ken Snyder

What is the feeling on adding functions to Date.prototype?  As mentioned 
in March (see message at bottom), adding Date.prototype.succ() would 
allow some helpful iterating.  I would suggest an optional argument to 
succ for time period and I also would like to throw out several more 
possible methods (below).

- Ken Snyder


// format
var d = new Date().format('%Y-%m-%d'); // 2007-08-17
// create(static)
Date.create('8-17-07').format('%Y-%m-%d'); // 2007-08-17
// add
var d = new Date().add(2, 'month').format('%Y-%m-%d'); // 2007-10-17
// diff
new Date().diff(d, 'month'); // 2
// succ
new Date().succ('year').format('%Y'); // 2008
// isEqual
new Date().isEqual('2007-08-17', 'day'); // true
// isBefore
new Date().isBefore('08-18-2007 7:00pm'); // true
// isAfter
new Date().isAfter('8/16/2007'); // true
// withinRange
new Date().withinRange('8/1/2007', '8-31-07'); // true
// daysInMonth (static)
Date.daysInMonth(2007, 2); // 28
// ISO (static property)
Date.create('8-17-2007').format(Date.ISO); // 2007-08-17 00:00:00


If the team is interested, I could refactor my current implementation 
for these functions and submit it as a patch for close review.  I 
understand users would probably want it lean and light if included at all.





2007-03-10 agrath wrote:
> ...
>
> This is a succ function for the date prototype and could be a worthy
> addition.
>
> Date.prototype.succ = function(){
>   return new Date(this.getFullYear(), this.getMonth(), this.getDate()
> +1);
> }
>
> ...



--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Prototype: Core" group.
To post to this group, send email to prototype-core@googlegroups.com
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
-~--~~~~--~~--~--~---