Skip to content

Instantly share code, notes, and snippets.

@almond-bongbong
Created April 4, 2019 06:24
Show Gist options
  • Save almond-bongbong/1e9410614c001a1e34530dc238d8f770 to your computer and use it in GitHub Desktop.
Save almond-bongbong/1e9410614c001a1e34530dc238d8f770 to your computer and use it in GitHub Desktop.
export const getTodayArray = (calc = 0) => {
const today = new Date();
const calcTime = new Date(today.getFullYear(), today.getMonth(), today.getDate() + calc);
const year = calcTime.getFullYear();
const month = calcTime.getMonth() + 1;
const day = calcTime.getDate();
return [year, month, day];
};
export const getYearMonth = ({ day = 0, month = 0 } = {}) => {
const today = getTodayArray();
const currentTime = new Date(today[0], today[1] + month, today[2] + day);
const newYear = currentTime.getFullYear();
const newMonth = `0${currentTime.getMonth()}`.slice(-2);
return `${newYear}-${newMonth}`;
};
export const getDay = () => `0${new Date().getDate()}`.slice(-2);
export const getCurrentMonthDate = (value) => {
const month = value.substr(5, 2);
const day = getDay();
const dayStr = `(${month}/01~${day})`;
return getYearMonth() === value ? dayStr : '';
};
export const getYesterdayMonthDate = () => {
const yesterday = getTodayArray(-1);
const month = yesterday[1];
const day = yesterday[2];
return `(${month}/01~${day})`;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment