如何在变量中使用今天的日期

时间:2016-12-16 14:01:46

标签: javascript json

我试图从JSON文档获取数据,并在文档中按日期排序..所以我希望我的页面加载今天的日期,所以我做了一个变量来获取今天的日期,但我不明白如何用它。我刚刚在sugString中添加了。但是这对我不起作用..任何人都知道我应该怎么做?这是我的代码!

link to JSON document

var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();

if(dd<10) {
  dd='0'+dd
} 

if(mm<10) {
  mm='0'+mm
} 

today = yyyy + '-' + mm + '-' + dd;

var ourRequest2 = new XMLHttpRequest();
ourRequest2.open('GET', 'https://api.opskins.com/IPricing/GetPriceList/v1/?appid=433850');
ourRequest2.onload = function() {
  var ourData2 = JSON.parse(ourRequest2.responseText);
  renderPinstripeJacketSug(ourData2)
};

ourRequest2.send();

function renderPinstripeJacketSug(data) {
  var sugString = data.response[ 'Skin: Pinstripe Suit Jacket' ].today.price;
  pinstripeJacketSug.insertAdjacentHTML('beforeend', sugstring);
}

2 个答案:

答案 0 :(得分:0)

您是否尝试过像这样访问...

var sugString = data.response['Skin: Pinstripe Suit Jacket'][today].price;

答案 1 :(得分:0)

尝试这样

&#13;
&#13;
var data = {"status":1,"time":1481897448,"response":{"Skin: Pinstripe Suit Jacke":{"2016-12-15":{"price":10},"2016-12-16":{"price":10}}}}

 // this data you have received in the renderPinstripeJacketSug call, below code will go inside this function only.

var today = new Date();
var dateFormatted = today.toISOString().slice(0,10);
var price = ""


Object.keys(data.response["Skin: Pinstripe Suit Jacke"]).map(function(key, index) {

     if(key == dateFormatted)
      {
           price = data.response["Skin: Pinstripe Suit Jacke"][key].price;
     }
});

var sugString = price;

console.log(sugString);

            
&#13;
&#13;
&#13;