Skip to content

Instantly share code, notes, and snippets.

@csandman
Created March 23, 2021 20:54
Show Gist options
  • Save csandman/a16c8eebf684455966f9cd0edd45314c to your computer and use it in GitHub Desktop.
Save csandman/a16c8eebf684455966f9cd0edd45314c to your computer and use it in GitHub Desktop.
// milliseconds in 1 second
export const MS_IN_S = 1000;
// seconds in 1 minute
export const S_IN_M = 60;
// minutes in 1 hour
export const M_IN_H = 60;
// hours in 1 day
export const H_IN_D = 24;
// days in 1 year
export const D_IN_Y = 365;
// milliseconds in 1 day
const MS_IN_D = MS_IN_S * S_IN_M * M_IN_H * H_IN_D;
export const dateAdd = (date, days) => {
const newDate = new Date(date);
newDate.setDate(newDate.getDate() + days);
return newDate;
};
export const addMonths = (date, months) => {
const d = date.getDate();
date.setMonth(date.getMonth() + +months);
if (date.getDate() !== d) {
date.setDate(0);
}
return date;
};
export const getYearBoundaries = (input) => {
let year = input;
if (input instanceof Date) {
year = input.getFullYear();
} else if (Number.isNaN(input) || !Number.isInteger) {
year = input.parseInt();
}
if (Number.isNaN(year)) {
return null;
}
return [new Date(Date.UTC(year, 0, 1)), new Date(year, 11, 31)];
};
export const getMonthBoundaries = (input) => {
let date = input;
if (!(input instanceof Date)) {
date = new Date(date);
}
const firstDay = new Date(Date.UTC(date.getFullYear(), date.getMonth(), 1));
const lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
return [firstDay, lastDay];
};
export const formatDate = (
input,
{ showWeekDay = false, month = 'short' } = {}
) => {
let date = input;
if (!(input instanceof Date)) {
date = new Date(input);
}
const opts = {
month,
day: 'numeric',
year: 'numeric',
};
if (showWeekDay) {
opts.weekday = 'short';
}
return date.toLocaleString('en-US', opts);
};
export const formatDateAsMonth = (input, options) => {
let date = input;
if (!(input instanceof Date)) {
date = new Date(input);
}
const opts = {
month: 'short',
year: 'numeric',
};
if (options?.showWeekDay) {
opts.weekday = 'short';
}
return date.toLocaleString('en-US', opts);
};
export const formatDateAsMonthInUtc = (input, options) => {
let date = input;
if (!(input instanceof Date)) {
date = new Date(input);
}
const utcDate = new Date(date);
utcDate.setTime(utcDate.getTime() + utcDate.getTimezoneOffset() * 60 * 1000);
return formatDateAsMonth(utcDate, options);
};
export const newUtcDate = (input) => {
const utcDate = new Date(input);
utcDate.setTime(utcDate.getTime() + utcDate.getTimezoneOffset() * 60 * 1000);
return utcDate;
};
export const formatDateInUtc = (input, options) => {
let date = input;
if (!(input instanceof Date)) {
date = new Date(input);
}
const utcDate = new Date(date);
utcDate.setTime(utcDate.getTime() + utcDate.getTimezoneOffset() * 60 * 1000);
return formatDate(utcDate, options);
};
export const getYearRange = (startDate, endDate, direction = 'asc') => {
let start = startDate;
let end = endDate;
if (!(start instanceof Date)) {
start = new Date(start);
}
if (!(end instanceof Date)) {
end = new Date(end);
}
let startYear = start.getFullYear();
let endYear = end.getFullYear();
const yearArr = [];
if (startYear > endYear) {
[startYear, endYear] = [endYear, startYear];
}
if (direction === 'desc') {
for (let i = endYear; i >= startYear; i -= 1) {
yearArr.push(i.toString());
}
} else {
for (let i = startYear; i <= endYear; i += 1) {
yearArr.push(i.toString());
}
}
return yearArr;
};
export const formatDateForInput = (date) => {
const d = new Date(date);
let month = `${d.getMonth() + 1}`;
let day = `${d.getDate()}`;
const year = d.getFullYear();
if (month.length < 2) {
month = `0${month}`;
}
if (day.length < 2) {
day = `0${day}`;
}
return `${year}-${month}-${day}`;
};
export const getFirstOfYear = () => new Date(new Date().getFullYear(), 0, 1);
export const getFirstOfMonth = () => {
const date = new Date();
return new Date(date.getFullYear(), date.getMonth(), 1);
};
export const getDaysBetween = (date1, date2) => {
const start = new Date(date1);
const end = new Date(date2);
const diff = Math.floor((end.getTime() - start.getTime()) / MS_IN_D);
return diff;
};
export const getDaysPassedInYear = (targetDate) => {
const firstOfYear = getFirstOfYear();
if (targetDate) {
return getDaysBetween(firstOfYear, new Date(targetDate));
}
const today = new Date();
const yesterday = new Date(today);
yesterday.setDate(yesterday.getDate() - 1);
return getDaysBetween(firstOfYear, yesterday);
};
export const getDateRangeOptions = (
startDate,
{
includeThisMonth = false,
includePastYear = false,
includeAllTime = false,
} = {}
) => {
const date = startDate ? new Date(startDate) : new Date();
let options = [];
if (includeThisMonth) {
options.push({
label: 'This Month',
value: {
startDate: getMonthBoundaries(new Date())[0],
endDate: getMonthBoundaries(new Date())[1],
},
});
}
if (includePastYear) {
options.push({
label: 'Past Year',
value: {
startDate: dateAdd(new Date(), -366),
endDate: new Date(),
},
});
}
const yearRange = getYearRange(date, new Date(), 'desc').map((year) => {
const boundaries = getYearBoundaries(year);
return {
label: year,
value: {
startDate: boundaries[0],
endDate: boundaries[1],
},
};
});
options = [...options, ...yearRange];
if (includeAllTime && getDaysBetween(date, new Date()) > 0) {
options.push({
label: 'All Time',
value: {
startDate: new Date(date),
endDate: new Date(),
},
});
}
return options;
};
export const getDaysInMonth = (targetDate) => {
const date = targetDate ? new Date(targetDate) : new Date();
return new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate();
};
export const getFormattedTimestamp = () => {
const today = new Date();
const y = today.getFullYear();
const m = today.getMonth() + 1;
const d = today.getDate();
const h = today.getHours();
const mi = today.getMinutes();
const s = today.getSeconds();
return `${y}-${m}-${d}_${h}-${mi}-${s}`;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment