timer.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * Copyright © 2010 Digia Plc
  3. * Copyright © 2010 Nokia Corporation
  4. *
  5. * All rights reserved.
  6. *
  7. * Nokia and Nokia Connecting People are registered trademarks of
  8. * Nokia Corporation.
  9. * Java and all Java-based marks are trademarks or registered
  10. * trademarks of
  11. * Sun Microsystems, Inc. Other product and company names
  12. * mentioned herein may be
  13. * trademarks or trade names of their respective owners.
  14. *
  15. *
  16. * Subject to the conditions below, you may, without charge:
  17. *
  18. * · Use, copy, modify and/or merge copies of this software and
  19. * associated documentation files (the "Software")
  20. *
  21. * · Publish, distribute, sub-licence and/or sell new software
  22. * derived from or incorporating the Software.
  23. *
  24. *
  25. * This file, unmodified, shall be included with all copies or
  26. * substantial portions
  27. * of the Software that are distributed in source code form.
  28. *
  29. * The Software cannot constitute the primary value of any new
  30. * software derived
  31. * from or incorporating the Software.
  32. *
  33. * Any person dealing with the Software shall not misrepresent
  34. * the source of the Software.
  35. *
  36. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
  37. * KIND, EXPRESS OR IMPLIED,
  38. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  39. * MERCHANTABILITY, FITNESS FOR A
  40. * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  41. * AUTHORS OR COPYRIGHT
  42. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  43. * WHETHER IN AN ACTION
  44. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  45. * CONNECTION WITH THE
  46. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  47. */
  48. /**
  49. * Returns current hours in two digits format
  50. */
  51. function getHours() {
  52. var today = new Date();
  53. if (today.getHours() < 10) return ("0" + today.getHours().toString());
  54. return today.getHours();
  55. }
  56. /**
  57. * Returns current minutes in two digits format
  58. */
  59. function getMinutes() {
  60. var today = new Date();
  61. if (today.getMinutes() < 10) return "0" + today.getMinutes().toString();
  62. return today.getMinutes();
  63. }
  64. /**
  65. * Returns current minutes in two digits format
  66. */
  67. function getSeconds() {
  68. var today = new Date();
  69. if (today.getSeconds() < 10) return "0" + today.getSeconds();
  70. return today.getSeconds();
  71. }
  72. /**
  73. * Returns current time in selected format
  74. *
  75. * @param hours If true hours are included to returned time
  76. * @param minutes If true minutes are included to returned time
  77. * @param seconds If true seconcs are included to returned time
  78. * @return current time in selected format
  79. */
  80. function getTimeJS(hours,minutes,seconds) {
  81. var time = "";
  82. if(hours) time += getHours();
  83. if(hours && minutes) time += ":"; //checks if there should be seperator character before minutes
  84. if(minutes) time += getMinutes() ;
  85. if((minutes || hours) && seconds ) time += ":" //checks if there should be seperator character seconds
  86. if(seconds) time += getSeconds();
  87. return time;
  88. }
  89. /**
  90. * Change number to two digit format. Fills empty space with "0" if necessary.
  91. *
  92. * @param number
  93. * @return number in two digits format
  94. * @example
  95. * formatTwoDigits(9) -> 09
  96. * formatTwoDigits(17) -> 17
  97. */
  98. function formatTwoDigits(number) {
  99. if(number < 10) return "0" + number;
  100. return number;
  101. }
  102. /*
  103. * Clever int parser for two digits numbers
  104. *
  105. * @param number Number to parse
  106. * @return parsed integer
  107. * @example
  108. * parseIntClever(09) -> 9
  109. * parseIntClever(29) -> 29
  110. */
  111. function parseIntClever(number) {
  112. if (number.length == 2 && number.charAt(0) == "0") return parseInt(number.charAt(1));
  113. return parseInt(number)
  114. }