Advertisement
Guest User

Untitled

a guest
Jan 30th, 2015
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. //app/mixins/dates.js
  2. import Ember from 'ember';
  3.  
  4. var Dates = Ember.Mixin.create({
  5. convert: function(d) {
  6. // Converts the date in d to a date-object. The input can be:
  7. // a date object: returned without modification
  8. // an array : Interpreted as [year,month,day]. NOTE: month is 0-11.
  9. // a number : Interpreted as number of milliseconds
  10. // since 1 Jan 1970 (a timestamp)
  11. // a string : Any format supported by the javascript engine, like
  12. // "YYYY/MM/DD", "MM/DD/YYYY", "Jan 31 2009" etc.
  13. // an object : Interpreted as an object with year, month and date
  14. // attributes. **NOTE** month is 0-11.
  15. return (
  16. d.constructor === Date ? d :
  17. d.constructor === Array ? new Date(d[0],d[1],d[2]) :
  18. d.constructor === Number ? new Date(d) :
  19. d.constructor === String ? new Date(d) :
  20. typeof d === "object" ? new Date(d.year,d.month,d.date) :
  21. NaN
  22. );
  23. },
  24. compare: function(a,b) {
  25. // Compare two dates (could be of any type supported by the convert
  26. // function above) and returns:
  27. // -1 : if a < b
  28. // 0 : if a = b
  29. // 1 : if a > b
  30. // NaN : if a or b is an illegal date
  31. // NOTE: The code inside isFinite does an assignment (=).
  32. return (
  33. isFinite(a=this.convert(a).valueOf()) &&
  34. isFinite(b=this.convert(b).valueOf()) ?
  35. (a>b)-(a<b) :
  36. NaN
  37. );
  38. },
  39. inRange: function(d,start,end) {
  40. // Checks if date in d is between dates in start and end.
  41. // Returns a boolean or NaN:
  42. // true : if d is between start and end (inclusive)
  43. // false : if d is before start or after end
  44. // NaN : if one or more of the dates is illegal.
  45. // NOTE: The code inside isFinite does an assignment (=).
  46. return (
  47. isFinite(d=this.convert(d).valueOf()) &&
  48. isFinite(start=this.convert(start).valueOf()) &&
  49. isFinite(end=this.convert(end).valueOf()) ?
  50. start <= d && d <= end :
  51. NaN
  52. );
  53. }
  54. });
  55.  
  56. export default Dates;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement