Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // got from https://kalender-365.de/leap-years.php should be updated accordingly
- const IS_LEAP_YEAR = {
- 1972: true,
- 1976: true,
- 1980: true,
- 1984: true,
- 1988: true,
- 1992: true,
- 1996: true,
- 2000: true,
- 2004: true,
- 2008: true,
- 2012: true,
- 2016: true,
- 2020: true,
- 2024: true,
- 2028: true,
- };
- const MONTH_DAYS = [
- -9999,
- 31,
- 28,
- 31,
- 30,
- 31,
- 30,
- 31,
- 31,
- 30,
- 31,
- 30,
- 31,
- 30,
- 31,
- ];
- const MONTH_NAMES = [
- "THERE_IS_NO_ZERO_MONTH",
- "January",
- "February",
- "March",
- "April",
- "May",
- "June",
- "July",
- "August",
- "September",
- "October",
- "November",
- "December",
- ];
- const DAY_NAMES = [
- "Sunday",
- "Monday",
- "Tuesday",
- "Wednesday",
- "Thursday",
- "Friday",
- "Saturday",
- ];
- function strToTime(time = 683475320, strFormat = "A d B Y") {
- let date = { year: 1970, month: 1, day: 1, hour: 0, minute: 0, second: 0 };
- //optimizer code
- let minutes = Math.floor(time / 60);
- date.second = time % 60;
- //first iteration was second-based, took too long, optimized x60!!!!
- for (let i = 0; i < minutes; i++) {
- // date.second += 1;
- // if (date.second > 59) {
- // date.second = 0;
- // date.minute += 1;
- // }
- date.minute += 1;
- if (date.minute > 59) {
- date.minute = 0;
- date.hour += 1;
- }
- if (date.hour > 23) {
- date.hour = 0;
- date.day += 1;
- }
- if (date.day > MONTH_DAYS[date.month]) {
- //gracefully checking for leap year
- if (date.month == 1 && IS_LEAP_YEAR[date.year]) {
- if (date.day > MONTH_DAYS[date.month] + 1) {
- date.day = 1;
- date.month += 1;
- }
- } else {
- date.day = 1;
- date.month += 1;
- }
- }
- if (date.month > 12) {
- date.month = 1;
- date.year += 1;
- }
- }
- return(
- strFormat
- .split("")
- .map((letter) => {
- if (letter == "d") {
- //padding
- return date.day.toString().length == 1 ? "0" + date.day : date.day;
- }
- if (letter == "A") {
- let dayOfTheWeek = DAY_NAMES[(Math.floor(time / 86400) % 7) + 4];
- //padding
- return dayOfTheWeek;
- }
- if (letter == "B") {
- //padding
- return MONTH_NAMES[date.month];
- }
- if (letter == "Y") {
- //padding
- return date.year.toString().length == 1 ? "0" + date.year : date.year;
- }
- if (letter == "H") {
- //padding
- return date.hour.toString().length == 1 ? "0" + date.hour : date.hour;
- }
- if (letter == "h") {
- let hour12 = date.hour % 12;
- if (hour12 == 0) hour12 = 12; //because on america 0 is 12
- //padding
- return hour12.toString().length == 1 ? "0" + hour12 : hour12;
- }
- if (letter == "M") {
- //padding
- return date.minute.toString().length == 1
- ? "0" + date.minute
- : date.minute;
- }
- if (letter == "S") {
- //padding
- return date.second.toString().length == 1
- ? "0" + date.second
- : date.second;
- }
- if (letter == "p") {
- return date.hour > 11 ? "pm" : "am";
- }
- return letter;
- })
- .join("")
- );
- }
- strToTime(1599335154, "h:M:S p");
- strToTime(Math.floor(new Date().getTime() / 1000), "A d B Y h:M:S p");
Add Comment
Please, Sign In to add comment