2

I believe this is a relatively simple question (a JavaScript noob here), but I can't seem to find a thread for this particular date function. I am doing website migration for an academic society from a PHP-based site to a drupal CMS. Some of the PHP has obviously broken and I'm trying to replace simple scripts with Javascript. One issue that is giving me a lot of trouble is how to get a text to appear only AFTER a certain date. In PHP my functioning code is:

<?php if (date('YmdH') > 2018011710 ) { ?>
    <p class="error">Please note that the deadline for submitting proposals has passed.</p>
<?php } ?>

So I need something in JavaScript to do the same. Here is what I came up with (I apologize in advance for my sloppy code as I'm a beginner with JavaScript):

First CSS to hide the DIV:

<style type="text/css">
    .DateDiv { display: none;}
</style>

Then the div itself:

<div class="DateDiv">
    <h3>Please note that the deadline for submitting proposals has passed.</h3>
</div>

Finally, my JavaScript, which is not working:

<script>
  $(document).ready(function() {
    var today = new Date();
    var dd = today.getDate();
    var mm = today.getMonth();
    var yyyy = today.getFullYear();

    if(dd<10) {
      dd = '0'+dd
    } 

    if(mm<10) {
      mm = '0'+mm
    } 

    today = mm + '/' + dd + '/' + yyyy;

    // show only if current date is after January 16, 20018
    if (today > 0, 16, 2018) {
      $(".DateDiv").show();
    }
  });
</script>

If anyone could help me sort this out I would be very grateful. If I'm going about this in a manner that is more complicated than it needs to be I'd also appreciate any advice.

Thanks in advance.

PS: I am not asking to compare two dates, but to display a text after a certain date.

Alexander
  • 90
  • 1
  • 3
  • 11
  • Take a look at https://stackoverflow.com/questions/492994/compare-two-dates-with-javascript – user3783243 May 28 '18 at 15:54
  • Implementing security checks in the frontend only isn't a good idea, everyone with a bit of knowledge could alter the check and submit after the deadline. – Appleshell May 28 '18 at 15:55
  • That condition ` (today > 0, 16, 2018)` does not look like valid JS code after all... – Nico Haase May 28 '18 at 15:59
  • Possible duplicate of [Compare two dates with JavaScript](https://stackoverflow.com/questions/492994/compare-two-dates-with-javascript) – Nico Haase May 28 '18 at 15:59
  • @Appleshell yes, but the CMS will not allow server-side code like PHP. I don't care if a user submits a proposal late as long as the text at the top of the page lets them know that the deadline has passed. Unless you have a better suggestion? – beingbecoming May 28 '18 at 16:03

2 Answers2

0

you just might want to do something like this:

if (new Date() >= new Date(2018, 0, 16))

months always start at 0 while days start at 1. don't ask why.

this is how the constructor is defined:

new Date(year, monthIndex [, day [, hour [, minutes [, seconds [, milliseconds]]]]]);

just go here for in-depth details about Date()

GottZ
  • 4,824
  • 1
  • 36
  • 46
0

//show only if current date is after January 16, 20018
var date_to_check_with = new Date("20180116").getTime();

//.getTime() will give time in milliseconds (epoch time) 
var current_date = new Date().getTime();

console.log(date_to_check_with < current_date);
Vivek
  • 1,465
  • 14
  • 29