Javascript对象日期值

时间:2015-08-12 21:14:15

标签: javascript date object

我在特征图像滑块中有一些图像。我需要按日期进行这些编程,这样我就不必在周末等网上更改这些内容。我想在对象中放置一个开始日期和结束日期。如何在对象中使用日期(而不是字符串)?

var bannerOne = {dateStart:"08/12/2015", dateEnd:"08/13/2015", banner:"<li><a href=\"https://unsplash.it/200/300\"><img src=\"https://unsplash.it/500/300\" /></a></li>"};
var bannerTwo = {dateStart:"08/14/2015", dateEnd:"08/15/2015", banner:"<li><a href=\"https://unsplash.it/200/300\"><img src=\"https://unsplash.it/600/300\" /></a></li>"};
var bannerThree = {dateStart:"08/12/2015", dateEnd:"08/13/2015", banner:"<li><a href=\"https://unsplash.it/200/300\"><img src=\"https://unsplash.it/550/300\" /></a></li>"};

我想比较当前日期,看看对象中的日期是否包含当前日期,如果是,则显示该图像。

我还是javascript的新手。

目前我使用此代码:

   <ul>   <script type="text/javascript">

  var banner = new Array();
banner[0] = "<li>First Statement</li>";
banner[1] = "<li>Second Statement</li>"; 
banner[2] = "<li>Third Statement</li>"; 
banner[3] = "<li>Fourth Statement</li>"; 
banner[4] = "<li>Fifth Statement</li>";


var d = new Date();
if (d.getDate() == 12)
{document.write(banner [0], banner [1], banner[2])}

/*Use this to program to be live during two dates !!!!LEAVE(-1)!!!!*/ else if (d.getMonth() == 8-1 && d.getDate() >= 10 && d.getDate() < 12) {document.write(banner [4], banner [1])}

else
{
document.write(banner [1], banner[2], banner [3], banner[4], banner [0]) }
</script>    </ul>

3 个答案:

答案 0 :(得分:0)

你应该真正考虑使用http://momentjs.com/你可以使用isBetween,isBefore,isAfter ......它非常灵活。

答案 1 :(得分:0)

您可以初始化这样的日期

var date = new Date("08/12/2015");

More Info

要比较两个日期,你可以这样做:

var date1 = new Date("08/12/2015");
var date2 = new Date("07/12/2015");
if( date1.getTime() > date2.getTime()) {
     console.log(date1);
}

答案 2 :(得分:0)

<!DOCTYPE html>
<html>
    <body>
        <script type="text/javascript">

            var bannerOne = {dateStart:"08/12/2015", dateEnd:"08/13/2015", banner:"<li><a href=\"https://unsplash.it/200/300\"><img src=\"https://unsplash.it/500/300\" /></a></li>"};
            var bannerTwo = {dateStart:"08/14/2015", dateEnd:"08/15/2015", banner:"<li><a href=\"https://unsplash.it/200/300\"><img src=\"https://unsplash.it/600/300\" /></a></li>"};
            var bannerThree = {dateStart:"08/12/2015", dateEnd:"08/13/2015", banner:"<li><a href=\"https://unsplash.it/200/300\"><img src=\"https://unsplash.it/550/300\" /></a></li>"};

            //Todays Date
            var currentDate = new Date();

            //setting minutes, hours and seconds of current date to 0
            currentDate.setMinutes(0);
            currentDate.setHours(0);
            currentDate.setSeconds(0);

            var start=new Date (bannerOne.dateStart);
            var end=new Date (bannerOne.dateEnd);

            //comparing the startdate with current date by substraction, if the subtraction result is less than 0(negative value) then the date is still valid and similarly checking for end date.
            if (start - currentDate < 0 && end - currentDate > 0){
            document.write(bannerOne.banner);
            }
            //Banner Two
            start=new Date (bannerTwo.dateStart);
            end=new Date (bannerTwo.dateEnd);

            if (start - currentDate < 0 && end - currentDate > 0){
            document.write(bannerTwo.banner);
            }
            //Banner Three
            start=new Date (bannerThree.dateStart);
            end=new Date (bannerThree.dateEnd);

            if (start - currentDate < 0 && end - currentDate > 0){
            document.write(bannerThree.banner);
            }

        </script> 
    </body>
<html>