繁体   English   中英

在本机反应中获取最近 6 个月的开始和结束日期 Javascript

[英]get last 6 months start and end date Javascript in react native

我想应用一个过滤器,我可以在其中显示从当前日期开始的最近六个月的交易,可以是任何月份

示例 #1 当前年份:

  • 2020 年 6 月(当月)
  • 2020 年 5 月
  • 2020 年 4 月
  • 2020 年 3 月
  • 2020 年 2 月
  • 2020 年 1 月

示例 #2 如果上一年是过去六个月:

  • 2020 年 3 月
  • 2020 年 2 月
  • 2020 年 1 月
  • 2019 年 12 月
  • 2019 年 11 月
  • 2019 年 10 月

所以我创建了 JS new Date object 并尝试使用下面提到的代码的方法,但不知道为什么在反应原生时得到 --------- Date { NaN }

这是我试图让这个工作但没有成功的方法

    let date = new Date()
    let year = date.getFullYear()
    const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];


    //Getting last 6 months from current date
    let currentMonth = date.getMonth(),
        previousMonth_1 = date.getMonth() - 1,
        previousMonth_2 = date.getMonth() - 2,
        previousMonth_3 = date.getMonth() - 3,
        previousMonth_4 = date.getMonth() - 4,
        previousMonth_5 = date.getMonth() - 5

    // getting total days of month
    const getDaysInMonth = (month, year) => {
        return new Date(year, month, 0).getDate();
    };


    //create function which will return the start and end object
    const getDate = (month) => {

        let addZeroInMonth = month < 10 ? `0${month + 1}` : month + 1

        return {
            startDate: new Date(`${year},${addZeroInMonth},1`),
            endDate: new Date(`${year},${addZeroInMonth},${getDaysInMonth(addZeroInMonth, year)}`),
        }
    }


    //Store each month
    let m1 = getDate(currentMonth)
    let m2 = getDate(previousMonth_1)
    let m3 = getDate(previousMonth_2)
    let m4 = getDate(previousMonth_3)
    let m5 = getDate(previousMonth_4)
    let m6 = getDate(previousMonth_5)



    //create array for last six months to pass in API on select
    let filter = [
        {
            label: 'Current',
            "startDate": `${m1.startDate} 00:00:00`,
            "endDate": `${m1.endDate} 00:00:00`,
        },
        {
            label: months[previousMonth_1] + ' ' + year,
            "startDate": `${m2.startDate} 00:00:00`,
            "endDate": `${m2.endDate} 00:00:00`,
        },
        {
            label: months[previousMonth_2] + ' ' + year,
            "startDate": `${m3.startDate} 00:00:00`,
            "endDate": `${m3.endDate} 00:00:00`,
        },
        {
            label: months[previousMonth_3] + ' ' + year,
            "startDate": `${m4.startDate} 00:00:00`,
            "endDate": `${m4.endDate} 00:00:00`,
        },
        {
            label: months[previousMonth_4] + ' ' + year,
            "startDate": `${m5.startDate} 00:00:00`,
            "endDate": `${m5.endDate} 00:00:00`,
        },
        {
            label: months[previousMonth_5] + ' ' + year,
            "startDate": `${m6.startDate} 00:00:00`,
            "endDate": `${m6.endDate} 00:00:00`,
        },
    ]

不幸的是,在本机反应中得到这个 output

 {"endDate": Date { NaN }, "startDate": Date { NaN }}
 {"endDate": Date { NaN }, "startDate": Date { NaN }}

但如果我在浏览器中运行这段代码,它工作正常,我也可以从中提取年、月、日

Wed Jan 01 2020 00:00:00 GMT+0530 (India Standard Time)
Fri Jan 31 2020 00:00:00 GMT+0530 (India Standard Time)

需要这样的东西在 API 中发送数据

LOG  {"endDate": "2020-06-30 00:00:00", "label": "Current", "startDate": "2020-06-01 00:00:00"}
LOG  {"endDate": "2020-05-31 00:00:00", "label": "May 2020", "startDate": "2020-05-01 00:00:00"}     
LOG  {"endDate": "2020-04-30 00:00:00", "label": "April 2020", "startDate": "2020-04-01 00:00:00"}   
LOG  {"endDate": "2020-03-31 00:00:00", "label": "March 2020", "startDate": "2020-03-01 00:00:00"}   
LOG  {"endDate": "2020-02-29 00:00:00", "label": "February 2020", "startDate": "2020-02-01 00:00:00"}
LOG  {"endDate": "2020-01-31 00:00:00", "label": "January 2020", "startDate": "2020-01-01 00:00:00"}

在此处输入图像描述

您可以使用 moment.js,这里是获取过去六个月的日期对象的小提琴。 var now = moment();

const endDate = now.format('MM-DD-YYYY')
const startDate = moment().date(1).month(now.month()).year(now.year()).format('MM-DD-YYYY')

console.log('sixth')

console.log(startDate);
console.log(endDate);

function getStartAndEndDate(month, year) {
    month = month - 1;
    year = month === -1 ? (year - 1) : year;
    const lastDate = moment().date(1).month(month).daysInMonth();

    return {
      startDate: moment().date(1).month(month).year(year).format('MM-DD-YYYY'),
      endDate: moment().date(lastDate).month(month).year(year).format('MM-DD-YYYY'),
      date: moment().date(1).month(month).year(year),
  }
}

const fifthMonth = getStartAndEndDate(now.month(), now.year());
console.log('fifth')

console.log(fifthMonth.startDate);
console.log(fifthMonth.endDate);

const fourthMonth = getStartAndEndDate(fifthMonth.date.month(), fifthMonth.date.year());
console.log('fourth')

console.log(fourthMonth.startDate);
console.log(fourthMonth.endDate);

const thirdMonth = getStartAndEndDate(fourthMonth.date.month(), fourthMonth.date.year());
console.log('third')

console.log(thirdMonth.startDate);
console.log(thirdMonth.endDate);

const secondMonth = getStartAndEndDate(thirdMonth.date.month(), thirdMonth.date.year());
console.log('second')

console.log(secondMonth.startDate);
console.log(secondMonth.endDate);

const firstMonth = getStartAndEndDate(secondMonth.date.month(), secondMonth.date.year());
console.log('firsth')

console.log(firstMonth.startDate);
console.log(firstMonth.endDate);

https://jsfiddle.net/khalid3n/yu1hbdw4/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM