timer.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Learn cc.Class:
  2. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
  3. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
  4. // Learn Attribute:
  5. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
  6. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
  7. // Learn life-cycle callbacks:
  8. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
  9. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
  10. cc.Class({
  11. extends: cc.Component,
  12. properties: {
  13. // foo: {
  14. // // ATTRIBUTES:
  15. // default: null, // The default value will be used only when the component attaching
  16. // // to a node for the first time
  17. // type: cc.SpriteFrame, // optional, default is typeof default
  18. // serializable: true, // optional, default is true
  19. // },
  20. // bar: {
  21. // get () {
  22. // return this._bar;
  23. // },
  24. // set (value) {
  25. // this._bar = value;
  26. // }
  27. // },
  28. },
  29. // LIFE-CYCLE CALLBACKS:
  30. onLoad: function () {
  31. this.type = 0;
  32. this.startTime();
  33. },
  34. startTime: function () {
  35. var myData = new Date();
  36. this.mytime = myData.getTime();
  37. },
  38. update: function (dt) {
  39. var testDate = new Date();
  40. // testDate.getYear(); //获取当前年份(2位)
  41. // testDate.getFullYear(); //获取完整的年份(4位,1970-????)
  42. // testDate.getMonth(); //获取当前月份(0-11,0代表1月)
  43. // testDate.getDate(); //获取当前日(1-31)
  44. // testDate.getDay(); //获取当前星期X(0-6,0代表星期天)
  45. var mytime = testDate.getTime(); //获取当前时间(从1970.1.1开始的毫秒数)
  46. // testDate.getHours(); //获取当前小时数(0-23)
  47. // testDate.getMinutes(); //获取当前分钟数(0-59)
  48. // testDate.getSeconds(); //获取当前秒数(0-59)
  49. // testDate.getMilliseconds(); //获取当前毫秒数(0-999)
  50. // testDate.toLocaleDateString(); //获取当前日期
  51. // var mytime=testDate.toLocaleTimeString(); //获取当前时间
  52. // testDate.toLocaleString( ); //获取日期与时间
  53. cc.log("开启计时器之后的毫秒", mytime - this.mytime);
  54. //直接调用公共JS里面的时间类处理的办法
  55. cc.log("格式化之后的计时器", this.dateFttM("ss:S",mytime - this.mytime));
  56. },
  57. dateFtt: function (fmt, date) { //author: meizz
  58. var o = {
  59. "M+": date.getMonth() + 1, //月份
  60. "d+": date.getDate(), //日
  61. "h+": date.getHours(), //小时
  62. "m+": date.getMinutes(), //分
  63. "s+": date.getSeconds(), //秒
  64. "q+": Math.floor((date.getMonth() + 3) / 3), //季度
  65. "S": date.getMilliseconds() //毫秒
  66. };
  67. if (/(y+)/.test(fmt))
  68. fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  69. for (var k in o)
  70. if (new RegExp("(" + k + ")").test(fmt))
  71. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  72. return fmt;
  73. },
  74. dateFttM: function (fmt, milliseconds) { //author: meizz
  75. var date = new Date(milliseconds);
  76. cc.log("这是什么啊", date);
  77. var o = {
  78. "M+": date.getMonth() + 1, //月份
  79. "d+": date.getDate(), //日
  80. "h+": date.getHours(), //小时
  81. "m+": date.getMinutes(), //分
  82. "s+": date.getSeconds(), //秒
  83. "q+": Math.floor((date.getMonth() + 3) / 3), //季度
  84. "S": date.getMilliseconds() //毫秒
  85. };
  86. if (/(y+)/.test(fmt))
  87. fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  88. for (var k in o)
  89. if (new RegExp("(" + k + ")").test(fmt))
  90. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  91. return fmt;
  92. }
  93. });