Datetime.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Licensed Materials - Property of IBM
  2. //
  3. // IBM Cognos Products: cogadmin
  4. //
  5. // (C) Copyright IBM Corp. 2005, 2011
  6. //
  7. // US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  8. //
  9. //
  10. dojo.provide("com.ibm.cognos.admin.utils.Datetime");
  11. /*
  12. * Add a feature to JS navtive datetime object to provide a mean to construct a datetime object on the
  13. * format of yyyy-mm-ddThh:mm:ss or yyyy-mm-dd
  14. * @param datetime, string with format of yyyy-mm-ddThh:mm:ss or yyyy-mm-dd
  15. */
  16. (function(){
  17. dojo.declare("com.ibm.cognos.admin.utils.Datetime",null,{
  18. constructor : function (datetime){
  19. this.datetime = datetime;
  20. },
  21. getObject : function (){
  22. //if empty return the now datetime object
  23. if (!this.datetime)
  24. return new Date();
  25. //if it's an date object already, just return it
  26. if (this.datetime instanceof Date)
  27. return this.datetime;
  28. //otherwise do the construction
  29. var aDatetime = this.datetime.split("T"),
  30. y = 0,
  31. m = 0,
  32. d = 0,
  33. h = 0,
  34. min = 0,
  35. s = 0;
  36. if (aDatetime[0]){
  37. var aDate = aDatetime[0].split("-");
  38. y = aDate[0];
  39. m = aDate[1] - 1;
  40. d = aDate[2];
  41. };
  42. if (aDatetime[1]){
  43. var aTime = aDatetime[1].split(":");
  44. h = aTime[0];
  45. min = aTime[1];
  46. s = aTime[2].substr(0,2);
  47. }
  48. return (new Date(y,m,d,h,min,s));
  49. },
  50. getDateString : function (){
  51. var o = this.getObject();
  52. var month = o.getMonth() + 1 + "";
  53. var day = o.getDate() + "";
  54. return o.getFullYear()+'-'+((month.length == 1)?'0':'')+month+'-'+((day.length == 1)?'0':'')+day;
  55. },
  56. getTimeString : function (){
  57. var o = this.getObject();
  58. var hour = o.getHours() + "";
  59. var min = o.getMinutes() + "";
  60. var sec = o.getSeconds() + "";
  61. return ((hour.length == 1)?'0':'')+hour+':'+((min.length == 1)?'0':'')+min+':'+((sec.length == 1)?'0':'')+sec;
  62. },
  63. getDateTimeString : function (){
  64. return this.getDateString() + 'T' + this.getTimeString();
  65. },
  66. isToday : function (){
  67. var today = new com.ibm.cognos.admin.utils.Datetime(new Date());
  68. return today.getDateString() == this.getDateString();
  69. }
  70. })
  71. })();