Sencha DateTime function


/ Published in: JavaScript
Save to your folder(s)

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


Copy this code and paste it in your HTML
  1. switch (dateTimeString)
  2. {
  3. /* DAY ********************************************************/
  4.  
  5. // get todays date-time, clear its time off
  6. case 'firstSecondOfToday':
  7. return Ext.Date.clearTime(new Date());
  8.  
  9. // take firstSecondOfToday and add 86399 seconds to it- there are 86400 seconds in a day
  10. case 'lastSecondOfToday':
  11. return Ext.Date.add(this.getDateTimeFor('firstSecondOfToday'), Ext.Date.SECOND, 86399);
  12.  
  13. /* WEEK ********************************************************/
  14.  
  15. // take the numeric representation of the day of the week (0-6) and subtract it from todaysFirstSecond
  16. // Add one to make the first day of the week the first second of Monday
  17. case 'firstSecondOfTheWeek':
  18. return Ext.Date.add(this.getDateTimeFor('firstSecondOfToday'), Ext.Date.DAY, -Ext.Date.format(this.getDateTimeFor('firstSecondOfToday'), 'N')+1);
  19.  
  20. // take first second of the week and subtract 7 days
  21. case 'firstSecondOfThePreviousWeek':
  22. return Ext.Date.add(this.getDateTimeFor('firstSecondOfTheWeek'), Ext.Date.DAY, -7);
  23.  
  24. // take the first second of the previous week and add number of seconds in a minus one
  25. case 'lastSecondOfThePreviousWeek':
  26. return Ext.Date.add(this.getDateTimeFor('firstSecondOfThePreviousWeek'), Ext.Date.SECOND, 604799);
  27.  
  28. /* MONTH ********************************************************/
  29.  
  30. case 'firstSecondOfTheMonth':
  31. return Ext.Date.getFirstDateOfMonth(new Date());
  32.  
  33. // take first second of the month and subtract one month
  34. case 'firstSecondOfThePreviousMonth':
  35. return Ext.Date.add(this.getDateTimeFor('firstSecondOfTheMonth'), Ext.Date.MONTH, -1);
  36.  
  37. // take the first second of the month and subtract one second
  38. case 'lastSecondOfThePreviousMonth':
  39. return Ext.Date.add(this.getDateTimeFor('firstSecondOfTheMonth'), Ext.Date.SECOND, -1);
  40.  
  41. /* QUARTER ********************************************************/
  42.  
  43. // take the current month number, get the remainder after three, add one to compensate for 0 based month numbers,
  44. // subtract from current month number
  45. case 'firstSecondOfTheQuarter':
  46. return Ext.Date.add(this.getDateTimeFor('firstSecondOfTheMonth'), Ext.Date.MONTH, -(Ext.Date.format(this.getDateTimeFor('firstSecondOfTheMonth'), 'n')%3)+1);
  47.  
  48. // take the first second of the quarter and subtract 3 months
  49. case 'firstSecondOfThePreviousQuarter':
  50. return Ext.Date.add(this.getDateTimeFor('firstSecondOfTheQuarter'), Ext.Date.MONTH, -3);
  51.  
  52. // take the first second of the current quarter and subtract one second
  53. case 'lastSecondOfThePreviousQuarter':
  54. return Ext.Date.add(this.getDateTimeFor('firstSecondOfTheQuarter'), Ext.Date.SECOND, -1);
  55.  
  56. /* YEAR ********************************************************/
  57.  
  58. // set month and date to january first and append year from firstSecondOfTheMonth
  59. // undefined time defaults to midnight AM
  60. case 'firstSecondOfTheYear':
  61. return new Date('1/1/'+Ext.Date.format(this.getDateTimeFor('firstSecondOfTheMonth'), 'Y'));
  62.  
  63. case 'firstSecondOfThePreviousYear':
  64. return Ext.Date.add(this.getDateTimeFor('firstSecondOfTheYear'), Ext.Date.YEAR, -1);
  65.  
  66. // take the first second of the year and subtract one second
  67. case 'lastSecondOfThePreviousYear':
  68. return Ext.Date.add(this.getDateTimeFor('firstSecondOfTheYear'), Ext.Date.SECOND, -1);
  69.  
  70. default:
  71. console.log('Error: invalid date called from static function getDateTimeFor() - '+dateTimeString);
  72. return -1;
  73. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.