puddlejumper26 / blogs

Personal Tech Blogs
4 stars 1 forks source link

Date() input with format: from yyyy-mm-ddt00-00-00.000z to yyyy-mm-ddt23-59-59.999z #41

Open puddlejumper26 opened 4 years ago

puddlejumper26 commented 4 years ago

Apart from the following complicated codes, we could use moment library directly

const GET_DATE = (new Date()).getDate();
const GET_MONTH = (new Date()).getMonth();
const GET_YEAR = (new Date()).getFullYear();
const GET_DATE_BEGIN = '00:00:00';
const GET_DATE_END = '23:59:59:999';
const MONTH_ARRAY = [
    'January',
    'Febuary',
    'March',
    'April',
    'May',
    'June',
    'July',
    'August',
    'Sepember',
    'October',
    'November',
    'December',
];

public toToday() {
        const getTodayBegin = new Date(`${MONTH_ARRAY[GET_MONTH]} ${GET_DATE} ${GET_YEAR} ${GET_DATE_BEGIN} UTC`);
        const getTodayEnd = new Date(`${MONTH_ARRAY[GET_MONTH]} ${GET_DATE} ${GET_YEAR} ${GET_DATE_END} UTC`);
        console.log(getTodayBegin.toISOString());
        console.log(getTodayEnd.toISOString());
    }

 public toYesterday() {
        const getTodayBegin = new Date(`${MONTH_ARRAY[GET_MONTH]} ${GET_DATE - 1} ${GET_YEAR} ${GET_DATE_BEGIN} UTC`);
        const getTodayEnd = new Date(`${MONTH_ARRAY[GET_MONTH]} ${GET_DATE - 1} ${GET_YEAR} ${GET_DATE_END} UTC`);
        console.log(getTodayBegin.toISOString());
        console.log(getTodayEnd.toISOString());
    }

    public toLastSevenDays() {
        const getTodayBegin = new Date(`${MONTH_ARRAY[GET_MONTH]} ${GET_DATE - 7} ${GET_YEAR} ${GET_DATE_BEGIN} UTC`);
        const getTodayEnd = new Date(`${MONTH_ARRAY[GET_MONTH]} ${GET_DATE} ${GET_YEAR} ${GET_DATE_END} UTC`);
        console.log(getTodayBegin.toISOString());
        console.log(getTodayEnd.toISOString());
    }

    public toThisMonth() {
        const getTodayBegin = new Date(`${MONTH_ARRAY[GET_MONTH]} ${'1'} ${GET_YEAR} ${GET_DATE_BEGIN} UTC`);
        const getTodayEnd = new Date(`${MONTH_ARRAY[GET_MONTH]} ${GET_DATE} ${GET_YEAR} ${GET_DATE_END} UTC`);
        console.log(getTodayBegin.toISOString());
        console.log(getTodayEnd.toISOString());
    }

    public toLastMonth() {
        let getLastMonth: number;
        if (GET_MONTH && GET_MONTH === 0) {
            getLastMonth = 11;
        } else {
            getLastMonth = GET_MONTH - 1;
        }

        const getLastMonthEnd = (lastMonth: number): number => {
            switch (lastMonth) {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    return 31;
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    return 30;
                    break;
                case 2:
                    return 28;
                    break;
            }
        };

        const getTodayBegin = new Date(`${MONTH_ARRAY[getLastMonth]}  ${'1'} ${GET_YEAR} ${GET_DATE_BEGIN} UTC`);
        const getTodayEnd = new Date(`${MONTH_ARRAY[getLastMonth]} ${getLastMonthEnd(getLastMonth + 1)} ${GET_YEAR} ${GET_DATE_END} UTC`);
        console.log(getTodayBegin.toISOString());
        console.log(getTodayEnd.toISOString());
    }