Guest User

Bad Code Challenge time formatter

a guest
Aug 31st, 2020
406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // got from https://kalender-365.de/leap-years.php should be updated accordingly
  2. const IS_LEAP_YEAR = {
  3.   1972: true,
  4.   1976: true,
  5.   1980: true,
  6.   1984: true,
  7.   1988: true,
  8.   1992: true,
  9.   1996: true,
  10.   2000: true,
  11.   2004: true,
  12.   2008: true,
  13.   2012: true,
  14.   2016: true,
  15.   2020: true,
  16.   2024: true,
  17.   2028: true,
  18. };
  19.  
  20. const MONTH_DAYS = [
  21.   -9999,
  22.   31,
  23.   28,
  24.   31,
  25.   30,
  26.   31,
  27.   30,
  28.   31,
  29.   31,
  30.   30,
  31.   31,
  32.   30,
  33.   31,
  34.   30,
  35.   31,
  36. ];
  37.  
  38. const MONTH_NAMES = [
  39.   "THERE_IS_NO_ZERO_MONTH",
  40.   "January",
  41.   "February",
  42.   "March",
  43.   "April",
  44.   "May",
  45.   "June",
  46.   "July",
  47.   "August",
  48.   "September",
  49.   "October",
  50.   "November",
  51.   "December",
  52. ];
  53.  
  54. const DAY_NAMES = [
  55.   "Sunday",
  56.   "Monday",
  57.   "Tuesday",
  58.   "Wednesday",
  59.   "Thursday",
  60.   "Friday",
  61.   "Saturday",
  62. ];
  63.  
  64. function strToTime(time = 683475320, strFormat = "A d B Y") {
  65.   let date = { year: 1970, month: 1, day: 1, hour: 0, minute: 0, second: 0 };
  66.  
  67.   //optimizer code
  68.   let minutes = Math.floor(time / 60);
  69.   date.second = time % 60;
  70.  
  71.   //first iteration was second-based, took too long, optimized x60!!!!
  72.   for (let i = 0; i < minutes; i++) {
  73.     // date.second += 1;
  74.  
  75.     // if (date.second > 59) {
  76.     //   date.second = 0;
  77.     //   date.minute += 1;
  78.     // }
  79.  
  80.     date.minute += 1;
  81.  
  82.     if (date.minute > 59) {
  83.       date.minute = 0;
  84.       date.hour += 1;
  85.     }
  86.  
  87.     if (date.hour > 23) {
  88.       date.hour = 0;
  89.       date.day += 1;
  90.     }
  91.  
  92.     if (date.day > MONTH_DAYS[date.month]) {
  93.       //gracefully checking for leap year
  94.       if (date.month == 1 && IS_LEAP_YEAR[date.year]) {
  95.         if (date.day > MONTH_DAYS[date.month] + 1) {
  96.           date.day = 1;
  97.           date.month += 1;
  98.         }
  99.       } else {
  100.         date.day = 1;
  101.         date.month += 1;
  102.       }
  103.     }
  104.  
  105.     if (date.month > 12) {
  106.       date.month = 1;
  107.       date.year += 1;
  108.     }
  109.   }
  110.  
  111.   return(
  112.     strFormat
  113.       .split("")
  114.       .map((letter) => {
  115.         if (letter == "d") {
  116.           //padding
  117.           return date.day.toString().length == 1 ? "0" + date.day : date.day;
  118.         }
  119.  
  120.         if (letter == "A") {
  121.           let dayOfTheWeek = DAY_NAMES[(Math.floor(time / 86400) % 7) + 4];
  122.           //padding
  123.           return dayOfTheWeek;
  124.         }
  125.  
  126.         if (letter == "B") {
  127.           //padding
  128.           return MONTH_NAMES[date.month];
  129.         }
  130.  
  131.         if (letter == "Y") {
  132.           //padding
  133.           return date.year.toString().length == 1 ? "0" + date.year : date.year;
  134.         }
  135.  
  136.         if (letter == "H") {
  137.           //padding
  138.           return date.hour.toString().length == 1 ? "0" + date.hour : date.hour;
  139.         }
  140.  
  141.         if (letter == "h") {
  142.           let hour12 = date.hour % 12;
  143.           if (hour12 == 0) hour12 = 12; //because on america 0 is 12
  144.           //padding
  145.           return hour12.toString().length == 1 ? "0" + hour12 : hour12;
  146.         }
  147.  
  148.         if (letter == "M") {
  149.           //padding
  150.           return date.minute.toString().length == 1
  151.             ? "0" + date.minute
  152.             : date.minute;
  153.         }
  154.  
  155.         if (letter == "S") {
  156.           //padding
  157.           return date.second.toString().length == 1
  158.             ? "0" + date.second
  159.             : date.second;
  160.         }
  161.  
  162.         if (letter == "p") {
  163.           return date.hour > 11 ? "pm" : "am";
  164.         }
  165.  
  166.         return letter;
  167.       })
  168.       .join("")
  169.   );
  170. }
  171.  
  172. strToTime(1599335154, "h:M:S p");
  173. strToTime(Math.floor(new Date().getTime() / 1000), "A d B Y h:M:S p");
Add Comment
Please, Sign In to add comment