按日期报价生成器

时间:2013-12-25 06:24:57

标签: javascript html

所以,我正在尝试制作一个报价生成器,根据日历年的日期生成不同的报价。这是我到目前为止所提出的:

<b id="texts">&nbsp;</b> 

<script type="text/javascript"> 
var quote = ["", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13",     "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25",
"26", "27", "28", "29", "30", "31"]; 
quote[0]=""; quote[1]="1"; quote[2]="2"; quote[3]="3"; quote[4]="4"; 
quote[5]="5"; 
quote[6]="6"; quote[7]="7"; quote[8]="8"; quote[9]="9"; 
quote[10]="10"; quote[11]="11"; 
quote[12]="12"; quote[13]="13"; quote[14]="14"; quote[15]="15"; 
quote[16]="16"; quote[17]="17"; 
quote[18]="18"; quote[19]="19"; quote[20]="20"; quote[21]="21"; 
quote[22]="22"; quote[23]="23"; 
quote[24]="24"; quote[25]="25"; quote[26]="26"; quote[27]="27"; 
quote[28]="28"; quote[29]="29"; 
quote[30]="30"; quote[31]="31";

function LoadDate() { 
        start = new Date("December 1,2014");
        future = new Date("December 31, 2013");
        range = []
        mil = 86400000 //24h
        for (var i=start.getTime(); i<future.getTime();i=i+mil) {

          range.push(new Date(i))
} 

window.onload = LoadDate; 
</script> 

不幸的是,我发现这个设置根本不起作用。在编码方面,我非常环保,所以我无法弄清问题是什么。我想我可能接近我想要的解决方案,但是我无法在这里解决松散的问题。帮助

1 个答案:

答案 0 :(得分:2)

我觉得你的代码很混乱。

我要做的是使用属性查找,如下所示:Live demo (click).

var dateObj = new Date();
var month = dateObj.getUTCMonth();
var day = dateObj.getUTCDate();
//month = 11;
//day = 25'

var quotes = {
  '11' : {
    '24': "It's Christmas Eve!",
    '25': 'Merry Christmas!',
    '26': 'Christmas is over.'
  }
};

var quoteElem = document.createElement('p');
quoteElem.textContent = quotes[month][day];

document.body.appendChild(quoteElem);

对象可以是数组。无论哪种方式..

在这里,我只是根据月份和日期查找报价,但这很容易适应任何事情。它仍然遵循使用日期通过它在对象/数组中的键来查找引用的原则。可能性非常大。

如果您想将引号与多个日期相关联,则可以将日期关联与引号相关联,然后查看与当天匹配的日期: Live demo (click). < / p>

var dateObj = new Date();
var month = dateObj.getUTCMonth();
var day = dateObj.getUTCDate();
//month = 11;
//day = 25'

var quotes = [
  {
    quote:"It's a day!",
    dates: [
      '11/25',
      '2/4'
    ]
  },
  {
    quote:"It's another day!",
    dates: [
      '11/26',
      '5/14'
    ]
  }
];

var date = month+'/'+day;
var quote;
for (var i=0; i<quotes.length; ++i) {
  if (quotes[i].dates.indexOf(date) !== -1) {
    quote = quotes[i].quote; 
  }
}

var quoteElem = document.createElement('p');
quoteElem.textContent = quote;

document.body.appendChild(quoteElem);