Last active
December 15, 2023 02:34
-
-
Save Juandresyn/f0ca331a60a473c84d692ed9034c5522 to your computer and use it in GitHub Desktop.
js collection of date related functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const initialDate = '07/29/2019'; | |
export const monthsArray = ['January','February','March','April','May','June','July','August','September','October','November','December']; | |
export const dayNames = ['Sun', 'Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat']; | |
/* | |
* @param padd Boolean - Add or not a 0 to the beginning of the number. | |
*/ | |
export const getWeekDate = (padd) => { | |
const currentDate = new Date(); // get current date | |
const first = currentDate.getDate() - currentDate.getDay() + 1; // First day is the day of the week (Monday) | |
const last = first + 6; // last day (friday) is the first day + 6 | |
const firstday = new Date(currentDate.setDate(first)).getDate(); | |
const lastday = new Date(currentDate.setDate(last)).getDate(); | |
if (padd) { | |
return `${firstday.toString().padStart(2, 0)} - ${lastday.toString().padStart(2, 0)}`; | |
} | |
return `${firstday} - ${lastday}`; | |
}; | |
/* | |
* This method gets the month (months) in the week. | |
*/ | |
export const getMonthsInWeek = () => { | |
const currentDate = new Date(); // get current date | |
const firstDay = currentDate.getDate() - currentDate.getDay() + 1; // First day of the week (Monday) | |
const lastDay = firstDay + 6; // last day (sunday) is the first day + 6 | |
const firstMonth = monthsArray[new Date(new Date().setDate(firstDay)).getMonth()]; | |
const lastMonth = monthsArray[new Date(new Date().setDate(lastDay)).getMonth()]; | |
return firstMonth === lastMonth ? `${firstMonth}` : `${firstMonth} - ${lastMonth}`; | |
}; | |
export const clock = (el) => { | |
let showColon = true; | |
const buildClock = (showColon = true) => { | |
const checkTime = (i) => i.toString().padStart(2, 0); | |
const today = new Date(); | |
const h = today.getHours(); | |
let m = today.getMinutes(); | |
m = checkTime(m); | |
const hTo12 = parseInt(h); | |
return `${ checkTime(hTo12 > 12 ? hTo12 - 12 : hTo12) }${showColon ? ':' : ' '}${m} ${hTo12 > 11 ? 'pm' : 'am'}`; | |
} | |
if (el) { | |
return setInterval(() => { | |
showColon = !showColon; | |
el.innerHTML = buildClock(showColon); | |
}, 1000); | |
} | |
return buildClock(); | |
} | |
export const currentDate = (date, dmy = false) => { | |
const current_datetime = new Date(); | |
if (dmy) { | |
return `${current_datetime.getDate().toString().padStart(2, 0)}/${(current_datetime.getMonth() + 1).toString().padStart(2, 0)}/${current_datetime.getFullYear()}`; | |
} | |
return `${(current_datetime.getMonth() + 1).toString().padStart(2, 0)}/${current_datetime.getDate().toString().padStart(2, 0)}/${current_datetime.getFullYear()}`; | |
} | |
export const isTodayOrFuture = (date) => { | |
if(new Date(date) >= new Date(currentDay())){ | |
return true | |
} | |
return false; | |
} | |
export const sortByDate = (arr) => arr.sort((a, b) => new Date(a.date) - new Date(b.date)); | |
export const sortByTime = (arr) => arr.sort((a, b) => new Date(`${a.date} ${a.hour}`) - new Date(`${b.date} ${b.hour}`)); | |
export const hoursDifference = (to, from) => { | |
const diff =(to.getTime() - from.getTime()) / 1000; | |
diff /= (60 * 60); | |
return Math.abs(Math.round(diff)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment