0

i have problem with displaying correct date. I want to display tommorow date if today is the last day in the month, and time is 20:00 PM and more.

I've tried this example, this and this but nothing worked for me.

function isLastDay(dt) {
      return new Date(dt.getTime() + 86400000).getDate() === 1;
  }

function changeDay() {
  var d = new Date();
  d.setHours(d.getHours() + (d.getTimezoneOffset() / 60) + 3);
  d.setDate(31);
  d.setHours(20);
  var day = d.getDate();
  var hrs = d.getHours();
  var min = d.getMinutes();
  var sec = d.getSeconds();

  var dayString = 'today';

  var mnt = new Array("january", "february", "march", "april", "may",
    "june", "july", "august", "september", "october", "november", "december");
  var lastday = new Date(new Date().getFullYear(), new Date().getMonth() + 1, 0).getDate();

  if (((hrs == 19) && (min > 45)) || (hrs > 19)) {
    day = day + 1;
    dayString = 'tomorrow';
  }

  if (day === lastday) {
    $(".start-web").html(day + " " + mnt[d.getMonth() + 1, 0]);
  } else {
    $(".start-web").html(day + " " + mnt[d.getMonth()]);
  }

  $(".start-web-d").html(dayString);
}

setInterval("changeDay()", 1000);
<div>
  <p>
    <strong>
      Start 
      <span class="start-web-d">&nbsp;</span>&nbsp;
      <span class="start-web">&nbsp;</span>&nbsp;
      20:00 
    </strong>
  </p>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

My script shows me 32 of January, for example, and my purpose is to show 1 of February. Any help would be appreciated, thanks.

Here is the link to my example.

  • You're adding `1` as an integer to `day` if the hours is between the range in the condition. Don't do that. Add a day to the Date object instead. – Rory McCrossan Jan 16 '19 at 15:44

4 Answers4

1

This logic should be all you need: if tomorrow is day 1 then today is the last day of the month, and if the date getHours method returns 20 or greater you should have met your criteria.

const dt = new Date();
const tomorrow = new Date(dt.getTime() + 60 * 60 * 24 * 1000).getDate();

if (tomorrow === 1 && dt.getHours() >= 20) {
  console.log('criteria met!');
} else {
  console.log(dt, tomorrow);
}
Nick
  • 16,066
  • 3
  • 16
  • 32
0

I see that you have the lastday variable there, but I cannot see how you are using it. You need to have something check to see if the current date is the 31st.

D. Foskett
  • 122
  • 1
  • 1
  • 9
0

Simply change the the === operator with >=

if (day >= lastday) {
  $(".start-web").html(day + " " + mnt[d.getMonth() + 1, 0]);
} else {
  $(".start-web").html(day + " " + mnt[d.getMonth()]);
}

Explanation

Before this line:

if (((hrs == 19) && (min > 45)) || (hrs > 19))

Your day value is 31, and then it adds the day value by 1 because the condition is met, so that after the if else scope, the day value is 32

Then you compare day which is 32 with lastday which is 31, therefore the condition is not met and showed 32 January instead.

whynut
  • 74
  • 2
0

The last Day of the month at 20:00PM is the same as the 1st day of the month -4 hours we can use that to create a function to check to see if we are past that date:

function lastMinus4(){
  let now = new Date();
  let lastM4 = new Date(now.getFullYear(), now.getMonth() + 1, 1, -4);

  return (now > lastM4);  
}

the now variable is the current date and time, the lastM4 is the current year, the current month + 1 which is next month, the 1 is the first day of the month, and the -4 is 4 hours in the past which is the last day of the month at 20:00PM. Another way would be to use the day as -1, and add set the hour to 20.

In the Date constructor you have above using 0 as the day gives you the last day of the previous month, so just add 20 at the end of the constructor for the hours and you have the time to check now against.