-1

I'm Doing Page Redirection Using end Date and Current Date , want to redirect page based on date , i did like this

issue: Redirection is not working

<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>
surya
  • 1
  • 4
  • Can you send console logs? – osmanraifgunes May 06 '19 at 11:31
  • Thanks for response, am bit new to coding, can you help me Thanks in advance @osmanraifgunes – surya May 06 '19 at 11:33
  • Possible duplicate of [Converting a string to a date in JavaScript](https://stackoverflow.com/questions/5619202/converting-a-string-to-a-date-in-javascript) – Alon Eitan May 06 '19 at 11:33
  • hint: if your endDate is the 7th of may 2019 you are using the wrong format for the date – Lelio Faieta May 06 '19 at 11:33
  • 2
    it seems your endDate is smaller then today. Your code will work tomorrow :) – osmanraifgunes May 06 '19 at 11:34
  • Thanks @LelioFaieta, if i enter format like yyyy-mm-dd , getting same issue page is not redirecting – surya May 06 '19 at 11:35
  • `new Date(07-05-2019);` is invalid syntax since it's not a string. You do get back a date object, but it's set to 1970. So comparing the dates never works, since endDate will always be the earliest date that can be exzpressed as a positive integer. Also, better compare the getTime() of the dates instead of just the dates themselves. When you compare them, they get cast to a string using `toString()` and lexical order is not the same as date time order for most locales. – Shilly May 06 '19 at 11:35
  • This is not even a valid date format, [read a tutorial](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) before asking – Alon Eitan May 06 '19 at 11:35

4 Answers4

0

Why don't you use milliseconds?

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 "";
  }
}
Reporter
  • 3,897
  • 5
  • 33
  • 47
0

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();
Shilly
  • 8,511
  • 1
  • 18
  • 24
0

your endDate new Date(07-05-2019) is invalid syntax. try like 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()
Syed mohamed aladeen
  • 6,507
  • 4
  • 32
  • 59
0

In your if-statement you are using the "new" keyword for variable endDate and curDate when you have already given both a value right above your if-block. Just use endDate and curDate as is.

Also check out how to create a date object on w3schools for the usage of new Date(). You might want to enclose your desired date in quotes, like a string.

So I did a thing:

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

...and html element for the onclick function:

<body>

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

</body>