如何从表达式更改日期和日期?

时间:2016-03-14 15:42:23

标签: javascript jquery regex

我从服务器获取json数据,其中我得到的日期表达式为“t + 1”,“t + 2”,“t-1”.. t 代表当前或系统日期。我需要将这些表达式更改为日期。如果t今天是2016-03-14。然后t-1 2016-03-13。我们可以在javascript中更改此表达式

这是我得到的对象 的输入

   [
      {
        "column": {
          "name": "a",
          "userId": "dipu"

        },
        "deal": {
          "dealNumbers": [],
          "fromDateExpression": {
            "expression": "t-1",
            "date": "2016-03-08"
          },
          "toDateExpression": {
            "expression": "t",
            "date": "2016-03-08"
          }
        }
      },
      {
        "column": {
          "name": "bb_test",
          "userId": "dipu"
        },
        "deal": {
          "dealNumbers": [
            "HH_2282825"
          ],
          "fromDateExpression": {
            "expression": "t+1",
            "date": "2016-03-09"
          },
          "toDateExpression": {
            "expression": "t+7",
            "date": "2016-03-17"
          }
        }
      },
      {
        "column": {
          "name": "my view",
          "userId": "dipu",
          "associatedWithAView": false
        },
        "deal": {
          "dealNumbers": [
            "HH_2282759"
          ],
          "fromDateExpression": {
            "expression": "t+2",
            "date": "2016-02-19"
          },
          "toDateExpression": {
            "expression": "t+3",
            "date": "2016-02-19"
          }
        }
      }
    ] 

我需要转换json数组。我试试这个

https://jsfiddle.net/e4nt1doz/1/

function changeDatefromExpress(expr){
if(expr=="t"){
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
return yyyy-mm-dd
}else(expr=="t+1"){

}
}

预期输出为

[
  {
    "column": {
      "name": "a",
      "userId": "dipu"

    },
    "deal": {
      "dealNumbers": [],
      "fromDateExpression": {
        "expression": "t-1",
        "date": "2016-03-13"
      },
      "toDateExpression": {
        "expression": "t",
        "date": "2016-03-14"
      }
    }
  },
  {
    "column": {
      "name": "bb_test",
      "userId": "dipu"
    },
    "deal": {
      "dealNumbers": [
        "HH_2282825"
      ],
      "fromDateExpression": {
        "expression": "t+1",
        "date": "2016-03-15"
      },
      "toDateExpression": {
        "expression": "t+7",
        "date": "2016-03-21"
      }
    }
  },
  {
    "column": {
      "name": "my view",
      "userId": "dipu",
      "associatedWithAView": false
    },
    "deal": {
      "dealNumbers": [
        "HH_2282759"
      ],
      "fromDateExpression": {
        "expression": "t+2",
        "date": "2016-03-16"
      },
      "toDateExpression": {
        "expression": "t+3",
        "date": "2016-02-17"
      }
    }
  }
]

我可以使用正则表达式吗...因为有很多案例,例如t+5t+6

1 个答案:

答案 0 :(得分:1)

https://jsfiddle.net/xxhg2bvc/

怎么样?

(我想说明一点 - 下面的代码完全正常工作)

Date.prototype.गांवारू=function(d){return new Date(this.valueOf()+864E5*d);};

function ლ_ಠ益ಠ_ლ(ಠ_ಠ){
    var π = ಠ_ಠ.getDate();
    var ಠ = ("0" + (ಠ_ಠ.getMonth() + 1)).slice(-2);
    var शैतानआरहाहै = ಠ_ಠ.getFullYear();
    return शैतानआरहाहै+'-'+ಠ+'-'+π;
}

var जावास्क्रिप्ट = data.map(function(el){

    var Hͫ̆̒̐ͣ̊̄ͯ͗͏̵̗̻̰̠̬͝ͅE̴̷̬͎̱̘͇͍̾ͦ͊͒͊̓̓̐_̫̠̱̩̭̤͈̑̎̋ͮͩ̒͑̾͋͘Ç̳͕̯̭̱̲̣̠̜͋̍O̴̦̗̯̹̼ͭ̐ͨ̊̈͘͠M̶̝̠̭̭̤̻͓͑̓̊ͣͤ̎͟͠E̢̞̮̹͍̞̳̣ͣͪ͐̈T̡̯̳̭̜̠͕͌̈́̽̿ͤ̿̅̑Ḧ̱̱̺̰̳̹̘̰́̏ͪ̂̽͂̀͠  = new Date();
      el.deal.fromDateExpression.date = ლ_ಠ益ಠ_ლ(Hͫ̆̒̐ͣ̊̄ͯ͗͏̵̗̻̰̠̬͝ͅE̴̷̬͎̱̘͇͍̾ͦ͊͒͊̓̓̐_̫̠̱̩̭̤͈̑̎̋ͮͩ̒͑̾͋͘Ç̳͕̯̭̱̲̣̠̜͋̍O̴̦̗̯̹̼ͭ̐ͨ̊̈͘͠M̶̝̠̭̭̤̻͓͑̓̊ͣͤ̎͟͠E̢̞̮̹͍̞̳̣ͣͪ͐̈T̡̯̳̭̜̠͕͌̈́̽̿ͤ̿̅̑Ḧ̱̱̺̰̳̹̘̰́̏ͪ̂̽͂̀͠ .गांवारू(el.deal.fromDateExpression.expression.match(/t(.*)/)[1]));
      el.deal.toDateExpression.date = ლ_ಠ益ಠ_ლ(Hͫ̆̒̐ͣ̊̄ͯ͗͏̵̗̻̰̠̬͝ͅE̴̷̬͎̱̘͇͍̾ͦ͊͒͊̓̓̐_̫̠̱̩̭̤͈̑̎̋ͮͩ̒͑̾͋͘Ç̳͕̯̭̱̲̣̠̜͋̍O̴̦̗̯̹̼ͭ̐ͨ̊̈͘͠M̶̝̠̭̭̤̻͓͑̓̊ͣͤ̎͟͠E̢̞̮̹͍̞̳̣ͣͪ͐̈T̡̯̳̭̜̠͕͌̈́̽̿ͤ̿̅̑Ḧ̱̱̺̰̳̹̘̰́̏ͪ̂̽͂̀͠ .गांवारू(el.deal.toDateExpression.expression.match(/t(.*)/)[1]));

      return el;
})
console.log(जावास्क्रिप्ट);