任何可能获得这些字符串“下一个”,“之后”,“即将来临”,“一个月后”,“下一个月”,“本月之后”的月份和日期

时间:2017-10-05 04:25:00

标签: javascript json node.js date bots

我在特定月份的json中有一个假期列表,用户只是询问“显示下个月假期”,“显示即将到来的假期”,“显示下一个2 /两个月假期”等等,这样我就是计划给出假期的选择,如何在JSON对象中找到指定月份的假期 我不知道,这是否可能使用Javascript日期对象或不... 下面是我的假期JSON对象

 {
"Month": "Jan",
"Date": 26,
"Day": "Thursday",
"Holiday": "Republic Day"
},
{
"Month": "Mar",
"Date": 13,
"Day": "Monday",
"Holiday": "Holi day"
},{
"Month": "Apr",
"Date": 14,
"Day": "Friday",
"Holiday": "Good Friday"
},
{
"Month": "May",
"Date": 1,
"Day": "Monday",
"Holiday": "May Day"
},{
"Month": "June",
"Date": 26,
"Day": "Monday",
"Holiday": "Ramzan(Eid-ul-fitr)***"
},
{
"Month": "Aug",
"Date": 7,
"Day": "Monday",
"Holiday": "Rksha Bandhan"
},
{
"Month": "Aug",
"Date": 14,
"Day": "Monday",
"Holiday": "Janmashtami"
}

请有人帮我跟踪用户值的json对象.. 我正在开发用于向用户展示假期的机器人..

1 个答案:

答案 0 :(得分:0)

当然,您可以先将月份名称缓存以匹配JSON中的名称:

var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

接下来的几个月:

var next = new Date();
next.setMonth(next.getMonth()+1); // If it is Nov now, expect to be Dec

var following = new Date();
following.setMonth(following.getMonth()+2); // If it is Nov now, expect to be Jan

然后迭代你的JSON以获得匹配的值:

for(i=0; i < holidays.length; i++){
  if(holidays[i].Month == monthNames[next.getMonth()]){
     //holidays[i] is a holiday in next month
  }
  else if(holidays[i].Month == monthNames[following.getMonth()]){
    //holidays[i] is a holiday in following month
  }
}

对于此示例,您还需要将JSON分配给假期变量,即:

var holidays = [
  {
    "Month": "Jan",
    "Date": 26,
    "Day": "Thursday",
    "Holiday": "Republic Day"
  },
  {
    "Month": "Mar",
    "Date": 13,
    "Day": "Monday",
    "Holiday": "Holi day"
  },
  {
    "Month": "Apr",
    "Date": 14,
    "Day": "Friday",
    "Holiday": "Good Friday"
  },
];

更新

要从用户选择中过滤为其添加字段,我将使用选择框作为示例:

<select name="filter" id="dateFilters">
  <option selected> - Select Filter - </option>
  <option value="next">Next Month</option>
  <option value="following">Following Month</option>
  <option value="next2">Next 2 Months</option>
</select>

然后使用所选值来决定要显示的内容:

var e = document.getElementById("dateFilters");
var selected = e.options[e.selectedIndex].value;

switch(selected){
  case 'next':
    var next = new Date();
    next.setMonth(next.getMonth()+1);

    var targets = [monthNames[next.getMonth()]];
    break;

  case 'following':
    var following = new Date();
    following.setMonth(following.getMonth()+2);

    var targets = [monthNames[following.getMonth()]];
    break;

  case 'next2':
    var next = new Date();
    next.setMonth(next.getMonth()+1);

    var following = new Date();
    following.setMonth(following.getMonth()+2);

    var targets = [monthNames[next.getMonth()], monthNames[following.getMonth()]];
    break;
}

然后迭代器变为:

for(i=0; i < holidays.length; i++){
  for(ii=0; ii < targets.length; ii++){
    if(holidays[i].Month == targets[ii]){
      // holidays[i] contains holiday to be shown
    }
  }
}