PDA

View Full Version : Date.getFirstDateOfWeek



wm003
3 Mar 2008, 4:51 AM
For my current project i needed a function to get the Date of the first Weekday of a week in which a given Date resides.


Date.prototype.getFirstDateOfWeek = function() {
var dayDiff = this.getDay()-(Ext.DatePicker?Ext.DatePicker.prototype.startDay:0);
if (dayDiff<0) dayDiff+=7;
return this.add(Date.DAY,-dayDiff);
};

DigitalSkyline
3 Mar 2008, 6:25 AM
How about and Ext version, like this?



Ext.apply(Date.prototype, {
getFirstDateOfWeek : function() {
var dt = this.clone();
return( dt.add('d', -dt.getDay()+1) );
});


~o)

wm003
3 Mar 2008, 6:43 AM
How about and Ext version, like this?



return( dt.add('d', -dt.getDay()+1) );
~o)

This will always return Monday. Fine for me as a german :), but as i have learned, weeks start with Sunday e.g. in USA, so i wanted it for more global usage. And the only place i found to get the value for the starting day of a week was in Datepicker. Correct me, if i'm wrong..

why did you clone the given date, btw? Wouldn't this run using "this" instead of "dt"?

DigitalSkyline
3 Mar 2008, 9:19 AM
Actually it came from a calendar app I'm working on and was derived from another date picker extension... so why this decision was made I'm not sure, but I think it had something to do with processing speed.

In any case, I think Ext is sorely lacking locale support. I've converted some Dojo functions to Ext that handles some of this, along with some more date functions, I can post them if you're interested.

wm003
3 Mar 2008, 10:57 PM
In any case, I think Ext is sorely lacking locale support. I've converted some Dojo functions to Ext that handles some of this, along with some more date functions, I can post them if you're interested.

Yes, would be nice, if you could post this here :D(i guess it's also interesting/useful for other forum members aswell)

DigitalSkyline
3 Mar 2008, 11:15 PM
Here are the various Date extensions I'm using in one package.

You might notice I updated the getFirstDateOfWeek to use the localization method I ported from Dojo. You can change the locale right there at the top, or programatically using setLocale(). Pretty much self-explanatory.





Ext.applyIf(Date.prototype, {
//see RFC 3066
locale : 'en-us',
setLocale : function(/*String?*/locale){
this.locale = this.normalizeLocale(locale);
},
// description:
// All variants are case-insensitive and are separated by '-' as specified in RFC 3066.
normalizeLocale : function(/*String?*/locale){
var result = locale ? locale.toLowerCase() : this.locale;
return result; // String
},
getFirstDateOfWeek : function() {
var dt = this.clone();
var dowOffset = this.getFirstDayOfWeek();
var dayOfWeek = (dt.getDay() + 6) % 7;
return( dt.add('d', -(dayOfWeek+dowOffset)) );
},
getLastDateOfWeek : function() {
var dt = this.getFirstDateOfWeek();
return( dt.add('d', 6 ) );
},
getFirstDayOfWeek : function(/*String?*/locale){
locale = locale || this.locale;
this.normalizeLocale(locale);
var firstDay = {/*default is 1=Monday*/
mv:5,
ae:6,af:6,bh:6,dj:6,dz:6,eg:6,er:6,et:6,iq:6,ir:6,jo:6,ke:6,kw:6,lb:6,ly:6,ma:6,om:6,qa:6,sa:6,
sd:6,so:6,tn:6,ye:6,
as:0,au:0,az:0,bw:0,ca:0,cn:0,fo:0,ge:0,gl:0,gu:0,hk:0,ie:0,il:0,is:0,jm:0,jp:0,kg:0,kr:0,la:0,
mh:0,mo:0,mp:0,mt:0,nz:0,ph:0,pk:0,sg:0,th:0,tt:0,tw:0,um:0,us:0,uz:0,vi:0,za:0,zw:0,
et:0,mw:0,ng:0,tj:0,
gb:0,
sy:4
};
var country = locale.split("-")[1];
var dow = firstDay[country];
return (typeof dow == 'undefined') ? 1 : dow; /*Number*/
},

// description:
// Returns a hash containing the start and end days of the weekend according to local custom using locale,
// or by default in the user's locale.
// e.g. {start:6, end:0}
getWeekend : function(/*String?*/locale){
locale = locale || this.locale;
this.normalizeLocale(locale);
var weekendStart = {/*default is 6=Saturday*/
eg:5,il:5,sy:5,
'in':0,
ae:4,bh:4,dz:4,iq:4,jo:4,kw:4,lb:4,ly:4,ma:4,om:4,qa:4,sa:4,sd:4,tn:4,ye:4
};
var weekendEnd = {/*default is 0=Sunday*/
ae:5,bh:5,dz:5,iq:5,jo:5,kw:5,lb:5,ly:5,ma:5,om:5,qa:5,sa:5,sd:5,tn:5,ye:5,af:5,ir:5,
eg:6,il:6,sy:6
};
var country = locale.split("-")[1];
var start = weekendStart[country];
var end = weekendEnd[country];
if(typeof start == 'undefined'){start=6;}
if(typeof end == 'undefined'){end=0;}
return {start:start, end:end}; /*Object {start,end}*/
},
// summary:
// Determines if the date falls on a weekend, according to local custom.
isWeekend :function(/*Date?*/dateObj, /*String?*/locale){
dateObj = dateObj || this.clone();
locale = locale || this.locale;
this.normalizeLocale(locale);
var weekend = this.getWeekend(locale);
var day = (dateObj || new Date()).getDay();
if(weekend.end<weekend.start){
weekend.end+=7;
if(day<weekend.start){ day+=7; }
}
return day >= weekend.start && day <= weekend.end; // Boolean
},

formatPretty: function(){
var dt = this.clone(),
diff = (((new Date()).getTime() - dt.getTime()) / 1000),
day_diff = Math.floor(diff / 86400);
if ( isNaN(day_diff) || day_diff <0 || day_diff>= 31 ) return;
return day_diff == 0 && (
diff <60 && "just now" ||
diff <120 && "1 minute ago" ||
diff <3600 && Math.floor( diff / 60 ) + " minutes ago" ||
diff <7200 && "1 hour ago" ||
diff <86400 && Math.floor( diff / 3600 ) + " hours ago") ||
day_diff == 1 && "Yesterday" ||
day_diff <7 && day_diff + " days ago" ||
day_diff <31 && Math.ceil( day_diff / 7 ) + " weeks ago";
},

//from ext docs
patterns : {
ISO8601Long:"Y-m-d H:i:s",
ISO8601Short:"Y-m-d",
ShortDate: "n/j/Y",
LongDate: "l, F d, Y",
FullDateTime: "l, F d, Y g:i:s A",
MonthDay: "F d",
SSTime: "H:i",
ShortTime: "g:i A",
LongTime: "g:i:s A",
SortableDateTime: "Y-m-d\\TH:i:s",
UniversalSortableDateTime: "Y-m-d H:i:sO",
YearMonth: "F, Y"
},
//convenience formatting function
formatPattern: function(pt) {
return this.format(this.patterns[pt])
}

});

wm003
3 Mar 2008, 11:26 PM
Indeed very useful!B) Thanks for sharing!