util.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. .pragma library
  2. function milliSecondsToString(milliseconds) {
  3. milliseconds = milliseconds > 0 ? milliseconds : 0;
  4. var timeInSeconds = Math.floor(milliseconds / 1000);
  5. var minutes = Math.floor(timeInSeconds / 60);
  6. var minutesString = minutes < 10 ? "0" + minutes : minutes;
  7. var seconds = Math.floor(timeInSeconds % 60);
  8. var secondsString = seconds < 10 ? "0" + seconds : seconds;
  9. return minutesString + ":" + secondsString;
  10. }
  11. function secondsToString(seconds) {
  12. return milliSecondsToString(seconds*1000);
  13. }
  14. function formatDate(dateString) {
  15. // Split the given dateString.
  16. // It's formatted in ISO 8601 (such as 2000-01-01T12:00:00.000Z).
  17. var dateStr = dateString.substring(0, 10);
  18. var timeStr = dateString.substring(11, dateString.length-1);
  19. var date = convertToDate(dateStr, timeStr);
  20. return date.getDate() + "/" + date.getMonth() + "/" + date.getFullYear();
  21. }
  22. function convertToDate(date, time) {
  23. // "Constant" definition. Define for the parseInt to use decimal system.
  24. var RADIX = 10;
  25. // Convert ISO 8601 representation of date to JS Date.
  26. var newDate = new Date(parseInt(date.substr(0,4), RADIX), // Parses the year (4 digits)
  27. parseInt(date.substr(5,2), RADIX)-1,// Parses the month (2 digits)
  28. parseInt(date.substr(8,2), RADIX)); // Parses the day (2 digits)
  29. // If time was given, add it as well.
  30. if(time) {
  31. // Set hours, 0-11
  32. newDate.setHours(parseInt(time.substr(0,2), RADIX));
  33. // Set minutes, 0-59
  34. newDate.setMinutes(parseInt(time.substr(3,2), RADIX));
  35. // Set seconds, 0-59
  36. newDate.setSeconds(parseInt(time.substr(6,2), RADIX));
  37. // Set milliseconds, 0-999
  38. newDate.setMilliseconds(parseInt(time.substr(9,2), RADIX));
  39. }
  40. return newDate;
  41. }