Advertisement
Guest User

24h -> AM-PM

a guest
Oct 9th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const TIME_SEP = "-";
  2. const AMPM_SEP = " ";
  3.  
  4. const reTimeSplit = /(.*?)(\d{2})-(\d{2})-(\d{2})(.*)/;
  5. const FULL_RSLT = 0, BEF_TIME = 1, TIME_HR = 2, TIME_MIN = 3, TIME_SEC = 4, AFT_TIME = 5;
  6.  
  7. const AM = 0, PM = 1;
  8. const AMPM = ["AM", "PM"];
  9.  
  10. var aTime = [];
  11.  
  12. // If the regex fails (return nothing) display an error and exit the script
  13. if (reTimeSplit.test(item.newBasename) == False) { return null; }
  14.  
  15. aTime = reTimeSplit.exec(item.newBasename);
  16. if ( parseInt(aTime[TIME_HR]) > 12 ){
  17. return aTime[BEF_TIME] + zpad((parseInt(aTime[TIME_HR]) - 12).toString(), 2) + TIME_SEP + aTime[TIME_MIN] + TIME_SEP + aTime[TIME_SEC] + AMPM_SEP + AMPM[PM] + aTime[AFT_TIME];}
  18. else{
  19. return aTime[BEF_TIME] + aTime[TIME_HR] + TIME_SEP + aTime[TIME_MIN] + TIME_SEP + aTime[TIME_SEC] + AMPM_SEP + AMPM[AM] + aTime[AFT_TIME];
  20. }
  21.  
  22. // Function that add leading zeros to any string
  23. function zpad(str, nb_digits)
  24. {
  25.     if (str.length < nb_digits)
  26.         { return (Array(nb_digits - str.length + 1).join('0') + str); }
  27.     else
  28.         { return str; }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement