基于日期的页面重定向以及如何将日期转换为字符串

时间:2019-05-06 11:28:17

标签: javascript jquery

我正在使用结束日期和当前日期进行页面重定向,想基于日期重定向页面,我确实如此

问题:重定向不起作用

<script type="text/javascript">
    function callFunc()
    {
       var endDate = new Date(07-05-2019);
       var curDate = new Date();
       if (new Date(endDate) > new Date(curDate))
       {
          window.location.replace('/AppName/page/page');
       } else {
          return "";
       }
   }
</script>

4 个答案:

答案 0 :(得分:0)

为什么不使用毫秒?

var endDate = new Date(2019, 4, 7);
// The target month is stored one less in Javascript
var var curDate = new Date();

if (endDate.getTime() > curDate.getTime())
{
    window.location.replace('/AppName/page/page');
 } else {
     return "";
  }
}

答案 1 :(得分:0)

[
  {
    "pickup_person":"SanPune6",
    "city":"pune",
    "office_address":"viman nagar",
    "office_city":"Pune",
    "office_state":"MH",
    "office_pincode":"121212",
    "pickup_email":"office@gmail.com",
    "preferred_start_time":"12:38:29",
    "preferred_end_time":"13:02:33",
    "mobile":"9090909090",
    "pickup_date":"12-04-2019"
  },
  {
    "pickup_person":"SanPune6",
    "city":"pune",
    "office_address":"viman nagar",
    "office_city":"Pune",
    "office_state":"MH",
    "office_pincode":"121212",
    "pickup_email":"office@gmail.com",
    "preferred_start_time":"12:38:29",
    "preferred_end_time":"13:02:33",
    "mobile":"9090909090",
    "pickup_date":"12-04-2019"
  }
]

答案 2 :(得分:0)

您的endDate new Date(07-05-2019)的语法无效。尝试像new Date(07-05-2019)

function callFunc()
    {
       var endDate = new Date('2019-05-07');
       var curDate = new Date();
       console.log(endDate, curDate)
       if (endDate > curDate)
       {
          window.location.replace('/AppName/page/page');
       } else {
          return "";
       }
   }
   
   callFunc()

答案 3 :(得分:0)

在if语句中,如果已经在if块上方都给出了两个值,则对变量endDate和curDate使用“ new”关键字。只需按原样使用endDate和curDate即可。

还可以在w3schools上查看how to create a date object,以了解新Date()的用法。您可能希望将所需的日期括在引号中,例如字符串。

所以我做了一件事情:

<script type="text/javascript">
// Wait for the page to load first
window.onload = function() {

//Get a reference to the link on the page
// with an id of "mylink"
var a = document.getElementById("mylink");
//Set code to run when the link is clicked
// by assigning a function to "onclick"
a.onclick = function() {

   var endDate = new Date("2019-05-10");
   var curDate = new Date();
   if (endDate > curDate)
   {
      window.location.replace('/AppName/page/page');
   } else {
      return "";
   }
}

</script>

...以及onclick函数的html元素:

<body>

<p>This is a clickable <span id="mylink">thing right here!</span></p>

</body>