home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2002 March / PCWMAR02.iso / software / windowsxp / ftgateoffice / ftgateoffice.exe / Main / time.js < prev    next >
Encoding:
Text File  |  2001-11-29  |  1.8 KB  |  104 lines

  1. var min = 60;
  2. var hour = min*60;
  3. var day = hour*24;
  4.  
  5. function getHours(time)
  6. {
  7.     var index = time.indexOf(":");
  8.  
  9.     return time.slice(0, index);
  10. }
  11.  
  12. function getMinutes(time)
  13. {
  14.     var index = time.indexOf(":");
  15.  
  16.     return     time.slice(index+1, 10);
  17. }
  18.  
  19. function isPeriod(period)
  20. {
  21.     var numExp = /^[0-9]+(s|m|d|h|$)$/
  22.  
  23.   return (period.search(numExp) != -1);
  24. }
  25.  
  26. function isTime(time)
  27. {
  28.     var index = time.indexOf(":");
  29.  
  30.     if (index<=0 || index==time.length-1)
  31.         return false;
  32.  
  33.     if (time.indexOf(".")>=0)
  34.         return false;
  35.  
  36.     var hours = time.slice(0, index);
  37.     var mins = time.slice(index+1, 10);
  38.  
  39.     var numExp=/^[0-9]+$/
  40.  
  41.     if (hours<0||hours>23||mins<0||mins>59||hours.search(numExp)==-1||mins.search(numExp)==-1)
  42.         return false;
  43.  
  44.     return true;
  45. }
  46.  
  47. function toNicePeriod(period)
  48. {
  49.     if (period==0)
  50.         return 0;
  51.     else if (period/day==Math.floor(period/day))
  52.         return (period/day)+"d";
  53.     else if (period/hour==Math.floor(period/hour))
  54.         return (period/hour)+"h";
  55.     else if (period/min==Math.floor(period/min))
  56.         return (period/min)+"m";
  57.     else
  58.         return period+"s";
  59. }
  60.  
  61. function fromNicePeriod(period)
  62. {
  63.     var c = period.charAt(period.length-1);
  64.  
  65.      if (c=="d" || c=="h" || c=="m" || c=="s")
  66.         period = period.substr(0, period.length-1);
  67.  
  68.     if (c=="d")
  69.         return eval(period)*day;
  70.     else if (c=="h")
  71.         return eval(period)*hour;
  72.     else if (c=="m")
  73.         return eval(period)*min;
  74.     else
  75.         return eval(period);
  76. }
  77.  
  78. function fromNiceTime(time)
  79. {
  80.     var min = 60;
  81.     var hour = min*60;
  82.     var day = hour*24;
  83.  
  84.     return getHours(time)*hour+getMinutes(time)*min;
  85. }
  86.  
  87. function toNiceTime(time)
  88. {
  89.     var hours=Math.floor(time/hour);
  90.     var mins=Math.floor((time-hours*hour)/min);
  91.  
  92.     if (hours<10)
  93.         time="0"+hours+":";
  94.     else
  95.         time=hours+":";
  96.  
  97.     if (mins<10)
  98.         time=time+"0"+mins;
  99.     else
  100.         time+=mins;
  101.  
  102.     return time;
  103. }
  104.