Not able to compare Fridays date JavaScript

manthan davda

Hi I have scenario where I need to get quantity of books sold in last two weeks. I am using Javascript for that. Everything was working fine until today. Comparison is failing when day is friday. Below is the example.

   //Function to get Monday of thisweek,last week and last to last week.
       function GetMonday(mday) {
         var today, todayNumber, mondayNumber, monday;
         today = new Date();
         todayNumber = today.getDay();
         mondayNumber = mday - todayNumber;
         return new Date(today.getFullYear(), today.getMonth(), today.getDate() + mondayNumber).toDateString();
}


  var currDate = new Date().toDateString(); // gets todays date
  var currwkDate = GetMonday(1); //Gets this week Monday's Date
  var lastwkDate = GetMonday(-6); //Gets last week Monday's Date
  var twowkDate = GetMonday(-13); //Gets Last to last week Monday's Date

  var BillDate=new Date("09/04/2015").toDateString(); // Friday (04 Sep)
          if (currDate == BillDate) {
                 alert("equal");
            }
            if (BillDate > currwkDate) {
                alert("this week");
            }
            if (BillDate > lastwkDate) {
                alert("last week");
            }
            if (BillDate > twowkDate) {
               alert("two week")
            }

Ideally above code should get three alert box(this week,last week,two week) buts its not giving even single alert.

If i edit above code and put BillDate to any other date which is not friday same code will work fine for example

   var BillDate= new Date("09/03/2015").toDateString(); //Thursday 03-Sep

I am not sure what is the problem please help!!!!!

tcooc

Your code is actually doing string comparisons rather than date comparisons. The reason this fails is because M from Monday is less than T, W, and S, but not F.

This makes it so that BillDate > currwkDate is only false if it's Friday. since F < M. The fix is to compare dates instead (remove uses of toDateString):

   //Function to get Monday of thisweek,last week and last to last week.
       function GetMonday(mday) {
         var today, todayNumber, mondayNumber, monday;
         today = new Date();
         todayNumber = today.getDay();
         mondayNumber = mday - todayNumber;
         return new Date(today.getFullYear(), today.getMonth(), today.getDate() + mondayNumber);
}


  var currDate = new Date(); // gets todays date
  var currwkDate = GetMonday(1); //Gets this week Monday's Date
  var lastwkDate = GetMonday(-6); //Gets last week Monday's Date
  var twowkDate = GetMonday(-13); //Gets Last to last week Monday's Date

  var BillDate=new Date("09/04/2015"); // Friday (04 Sep)
          if (currDate.getYear() == BillDate.getYear() && currDate.getMonth() == BillDate.getMonth() && currDate.getDate() == BillDate.getDate()) {
                 alert("equal");
            }
            if (BillDate > currwkDate) {
                alert("this week");
            }
            if (BillDate > lastwkDate) {
                alert("last week");
            }
            if (BillDate > twowkDate) {
               alert("two week")
            }

Edit: The first check for dates to be equal also needs to be modified to only check year, month, and date (ignore hours/etc.).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Get all Fridays in a date Range in Java

Date compare in javascript not working

Django (1.11) ORM using postgresql query is not able to compare date

Compare only time in Date - javascript

Compare date not working with toLocaleDateString() in javascript?

Javascript new Date(), compare confuse

Compare Date arrays in loop in Javascript

Compare Javascript date with php current date

How to compare today date with another date in javascript?

Get the Date Difference Between two dates excluding fridays in SQL Server

How to display all Fridays Date between two dates

JavaScript if statement able to compare two variables with identical values

JavaScript date compare fails but prints same value

How to compare date string using javascript

Why date compare javascript function is not working?

Javascript / Typescript: Compare Date, string, and Moment variables

How to compare two different date formats in javascript

How do I add days to the current date field to be able to compare range of dates

Can not compare the given date with today date using Javascript

Javascript: Compare current date converted to UTC with date string

Hash states to be able to compare them

Not able to compare day in momentjs with isSame()

Not able to compare NAN using Expression

Python not able to compare the dates in dateframes

Not able to insert date in JDBC

Not able to convert into date in oracle

Date not able to Format(OOP)

How do I compare a JSON date string to javascript string?

How to compare current time only without date to a given time in javascript