首页 > 解决方案 > 基于日期的页面重定向以及如何将日期转换为字符串

问题描述

我正在使用结束日期和当前日期进行页面重定向,想根据日期重定向页面,我确实喜欢这个

问题:重定向不起作用

<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>

标签: javascriptjquery

解决方案


function callFunc() {
  // Has to be a valid string to get parsed.
  var endDate = new Date( '2019-05-07' );
  var curDate = new Date();
  // No reason to create a new Date() from something that is already a Date object.
  // Use getTime() to get an integer representing the datetime.
  // Compare those instead of the Date objects to avoid issues.
  if ( endDate.getTime() > curDate.getTime() ) {
    console.log( 'change location' );
  } else {
    console.log( 'do nothing' );
  }
}

callFunc();


推荐阅读