/ Published in: JavaScript
pass in a string representation for a time period and it returns a date object; Weeks are assumed to be Monday midnight Am - Sunday 23:59:59
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
switch (dateTimeString) { /* DAY ********************************************************/ // get todays date-time, clear its time off case 'firstSecondOfToday': return Ext.Date.clearTime(new Date()); // take firstSecondOfToday and add 86399 seconds to it- there are 86400 seconds in a day case 'lastSecondOfToday': return Ext.Date.add(this.getDateTimeFor('firstSecondOfToday'), Ext.Date.SECOND, 86399); /* WEEK ********************************************************/ // take the numeric representation of the day of the week (0-6) and subtract it from todaysFirstSecond // Add one to make the first day of the week the first second of Monday case 'firstSecondOfTheWeek': return Ext.Date.add(this.getDateTimeFor('firstSecondOfToday'), Ext.Date.DAY, -Ext.Date.format(this.getDateTimeFor('firstSecondOfToday'), 'N')+1); // take first second of the week and subtract 7 days case 'firstSecondOfThePreviousWeek': return Ext.Date.add(this.getDateTimeFor('firstSecondOfTheWeek'), Ext.Date.DAY, -7); // take the first second of the previous week and add number of seconds in a minus one case 'lastSecondOfThePreviousWeek': return Ext.Date.add(this.getDateTimeFor('firstSecondOfThePreviousWeek'), Ext.Date.SECOND, 604799); /* MONTH ********************************************************/ case 'firstSecondOfTheMonth': return Ext.Date.getFirstDateOfMonth(new Date()); // take first second of the month and subtract one month case 'firstSecondOfThePreviousMonth': return Ext.Date.add(this.getDateTimeFor('firstSecondOfTheMonth'), Ext.Date.MONTH, -1); // take the first second of the month and subtract one second case 'lastSecondOfThePreviousMonth': return Ext.Date.add(this.getDateTimeFor('firstSecondOfTheMonth'), Ext.Date.SECOND, -1); /* QUARTER ********************************************************/ // take the current month number, get the remainder after three, add one to compensate for 0 based month numbers, // subtract from current month number case 'firstSecondOfTheQuarter': return Ext.Date.add(this.getDateTimeFor('firstSecondOfTheMonth'), Ext.Date.MONTH, -(Ext.Date.format(this.getDateTimeFor('firstSecondOfTheMonth'), 'n')%3)+1); // take the first second of the quarter and subtract 3 months case 'firstSecondOfThePreviousQuarter': return Ext.Date.add(this.getDateTimeFor('firstSecondOfTheQuarter'), Ext.Date.MONTH, -3); // take the first second of the current quarter and subtract one second case 'lastSecondOfThePreviousQuarter': return Ext.Date.add(this.getDateTimeFor('firstSecondOfTheQuarter'), Ext.Date.SECOND, -1); /* YEAR ********************************************************/ // set month and date to january first and append year from firstSecondOfTheMonth // undefined time defaults to midnight AM case 'firstSecondOfTheYear': return new Date('1/1/'+Ext.Date.format(this.getDateTimeFor('firstSecondOfTheMonth'), 'Y')); case 'firstSecondOfThePreviousYear': return Ext.Date.add(this.getDateTimeFor('firstSecondOfTheYear'), Ext.Date.YEAR, -1); // take the first second of the year and subtract one second case 'lastSecondOfThePreviousYear': return Ext.Date.add(this.getDateTimeFor('firstSecondOfTheYear'), Ext.Date.SECOND, -1); default: console.log('Error: invalid date called from static function getDateTimeFor() - '+dateTimeString); return -1; }