首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >科普特型日历组件反应本机

科普特型日历组件反应本机
EN

Stack Overflow用户
提问于 2020-10-20 18:19:01
回答 1查看 161关注 0票数 1

我正在尝试实现一个非公历,它有13个月。我用了两个函数来做这个。他们中的一个

generateCalendar用于为每个月创建一个日历矩阵,并以周天数为索引。

代码语言:javascript
复制
function generateCalendar(type = 'en') {
    var [etYear, etMonth, etDate] = Ethiopic.toEthiopian(
      state.activeDate.getFullYear(),
      state.activeDate.getMonth() + 1,
      state.activeDate.getDate(),
    );
    console.log('Month: ', etMonth);

    var enYear = state.activeDate.getFullYear();
    var enMonth = state.activeDate.getMonth();

    var matrix = [];

    if (type == 'en') {
      matrix = [];
      var firstDay = new Date(enYear, enMonth, 1).getDay();
      var maxDays = enCalendar.en_days[enMonth];
      if (enMonth == 1) {
        if ((enYear % 4 == 0 && enYear % 100 != 0) || enYear % 400 == 0) {
          maxDays += 1;
        }
      }

      matrix[0] = enCalendar.en_weekDays;

      var counter = 1;
      for (var row = 1; row < 7; row++) {
        matrix[row] = [];
        for (var col = 0; col < 7; col++) {
          matrix[row][col] = -1;
          if (row == 1 && col >= firstDay) {
            matrix[row][col] = counter++;
          } else if (row > 1 && counter <= maxDays) {
            matrix[row][col] = counter++;
          }
        }
      }
    } else if (type == 'et') {
      matrix = [];
      var startDayOfYear = Ethiopic.startDayOfEthiopian(etYear);

      // var firstDay = startOfYear + (30 % startDayOfYear);
      var firstDayOfYear = new Date(enYear, 8, startDayOfYear).getDay();
      var firstDay =
        (etMonth - 1) * 2 + firstDayOfYear > 7
          ? ((etMonth - 1) * 2 + firstDayOfYear) % 7
          : (etMonth - 1) * 2 + firstDayOfYear;
      var maxDays = etCalendar.et_days[etMonth - 1];
      console.log(maxDays);
      if (etMonth == 13) {
        if (etYear % 4 == 3) {
          maxDays += 1;
        }
      }

      matrix[0] = etCalendar.et_weekDays;

      var counter = 1;
      for (var row = 1; row < 7; row++) {
        matrix[row] = [];
        for (var col = 0; col < 7; col++) {
          matrix[row][col] = -1;
          if (row == 1 && col >= firstDay) {
            matrix[row][col] = counter++;
          } else if (row > 1 && counter <= maxDays) {
            matrix[row][col] = counter++;
          }
        }
      }
    }
    return matrix;
  }

另一个是changeMonth,它增加了一个月并改变了状态,从而创建了一个新的矩阵。

代码语言:javascript
复制
function changeMonth(n) {
    setState({
       activeDate: new Date(state.activeDate.setMonth(state.activeDate.getMonth() + n))
      });
  }

这里的问题是埃塞俄比亚的日历有13个月,所以我不能使用setmonth和递增或递减,然后转换回埃塞俄比亚,因为activeDate确定的月数是12。所以So月在11之后重置,并且只工作12个月。

但是,由于埃塞俄比亚历法中的每个月都是30天,除了最后一个月有5或6天外,我想把它做好,这样它就能在增加或减少月份的月份中循环,然后我也许可以转换回公历,设置activeDate。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-21 13:49:46

您需要解释添加科普特月的规则是什么。

例如,在1个月到11个月的日期中添加一个月,只需增加30天。对于12个月的日期,加上30天可能会将日期推后到第13个月之后,因此进入下一年的第一个月。在这种情况下,该日期是否应限于13日的最后一天?因此,将一个月延长到1737年9月5日,而不是1738年1月25日(30天后)。

同样,将一个月加到1/13/1737是1/01/1738 (即下个月的同一天)还是26/01/1738 (30天后)?

给定本机ECMAScript日期,您可以按以下顺序添加一个科普特月:

如果科普特月为1到11,则

  1. 获得等效的科普特日期
  2. ;如果科普特月为12,则添加30天
  3. ;如果超过13月底,则将其设置为13
  4. 的最后一天(如果科普特月为13),将该日期设置为下一个月

中的同一天。

例如。

代码语言:javascript
复制
// Given a date, return an object with era, year, month
// and day properties and values for the equivalent
// Coptic date
function getCopticDate(date = new Date()) {
  return new Intl.DateTimeFormat(
    'en-u-ca-coptic', {
      year : 'numeric',
      month: '2-digit',
      day  : '2-digit'
    }).formatToParts(date).reduce((acc, part) => {
      if (part.type != 'literal') {
        acc[part.type] = part.value;
      }
      return acc;
    }, Object.create(null));
}

// Given a Date, return a string for the equivalent
// Coptic date
function printCopticDate(date = new Date()) {
  let {era, year, month, day} = getCopticDate(date);
  return `${day}/${month}/${year} ${era}`;
}

// Given a Date, return a Date with one Coptic month
// added to to the equivalent Coptic date
function addCopticMonth(date) {
  let oDate = new Date(date);
  let d = getCopticDate(oDate);
  // Add 30 days, then deal with months 12 and 13
  oDate.setDate(oDate.getDate() + 30)
  // If month was 12 and is now not 13, set to
  // last day of 13
  let e = getCopticDate(oDate);
  if (d.month == '12' && e.month != '13') {
    oDate.setDate(oDate.getDate() - e.day);
  }
  // If month was 13, set to same day in next month
  if (d.month == '13') {
    oDate.setDate(oDate.getDate() - e.day + +d.day) 
  }
  return oDate;
}

// Some tests
// Add 1 month to today
let d = new Date();
console.log('Today is: ' + printCopticDate(d) + 
  ' (' + d.toDateString() + ')');
let e = addCopticMonth(d);
console.log('+1 month: ' + printCopticDate(e) + 
  ' (' + e.toDateString() + ')');

// Add 1 month to 20/12/1737 -> last day of 13th month
let f = new Date(2021, 7, 26);
let g = addCopticMonth(f);
console.log(printCopticDate(f) + ' (' + 
  f.toDateString() + ') +1 month is\n' +
  printCopticDate(g) + ' (' + g.toDateString() + ')');
  
// Add 1 month to 05/13/1737 -> same day in first month of next year  
let h = addCopticMonth(g);
console.log(printCopticDate(g) + ' (' + 
  g.toDateString() + ') +1 month is\n' +
  printCopticDate(h) + ' (' + h.toDateString() + ')');

上面的代码利用了并涵盖了添加一个月,如果您想要添加多个年或多个年份,则需要做更多的…工作它实际上只是演示一个算法,而不是提供一个健壮的函数(例如,getCopticDate应该被称为getCopticDateParts,值应该是数字,而不是字符串等等)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64451145

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档