Guest User

Untitled

a guest
Nov 17th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. /*
  2. * Javascript time conversion script
  3. * version 1.0
  4. * @requires jQuery
  5. * @author Modern Tribe, Inc. (Peter Chester)
  6. *
  7. * Converts several formats to a 2 point float. Accepted formats include:
  8. * 1.3333333333
  9. * 1h 10m
  10. * 1:10
  11. * 1
  12. *
  13. * Entries over 15 hours are assumed to be 15 minutes.
  14. */
  15.  
  16. function filterTimeFormat(time) {
  17.  
  18. // Number of decimal places to round to
  19. var decimal_places = 2;
  20.  
  21. // Maximum number of hours before we should assume minutes were intended. Set to 0 to remove the maximum.
  22. var maximum_hours = 15;
  23.  
  24. // 3
  25. var int_format = time.match(/^\d+$/);
  26.  
  27. // 1:15
  28. var time_format = time.match(/([\d]*):([\d]+)/);
  29.  
  30. // 10m
  31. var minute_string_format = time.toLowerCase().match(/([\d]+)m/);
  32.  
  33. // 2h
  34. var hour_string_format = time.toLowerCase().match(/([\d]+)h/);
  35.  
  36. if (time_format != null) {
  37. hours = parseInt(time_format[1]);
  38. minutes = parseFloat(time_format[2]/60);
  39. time = hours + minutes;
  40. } else if (minute_string_format != null || hour_string_format != null) {
  41. if (hour_string_format != null) {
  42. hours = parseInt(hour_string_format[1]);
  43. } else {
  44. hours = 0;
  45. }
  46. if (minute_string_format != null) {
  47. minutes = parseFloat(minute_string_format[1]/60);
  48. } else {
  49. minutes = 0;
  50. }
  51. time = hours + minutes;
  52. } else if (int_format != null) {
  53. // Entries over 15 hours are likely intended to be minutes.
  54. time = parseInt(time);
  55. if (maximum_hours > 0 && time > maximum_hours) {
  56. time = (time/60).toFixed(decimal_places);
  57. }
  58. }
  59.  
  60. // make sure what ever we return is a 2 digit float
  61. time = parseFloat(time).toFixed(decimal_places);
  62.  
  63. return time;
  64. }
Add Comment
Please, Sign In to add comment