28

I've got a string from an input field which I use for date with a format like this 25-02-2013. Now I want to compare the string with today's date. I want to know if the string is older or newer then today's date.

Any suggestions?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Leon van der Veen
  • 1,652
  • 11
  • 42
  • 60

9 Answers9

21
    <script type="text/javascript">

var q = new Date();
var m = q.getMonth()+1;
var d = q.getDay();
var y = q.getFullYear();

var date = new Date(y,m,d);

mydate=new Date('2011-04-11');
console.log(date);
console.log(mydate)

if(date>mydate)
{
    alert("greater");
}
else
{
    alert("smaller")
}


</script>
Gaurav Dave
  • 6,838
  • 9
  • 25
  • 39
polin
  • 2,745
  • 2
  • 15
  • 20
16

Exact date comparsion and resolved bug from accepted answer

var q = new Date();
var m = q.getMonth();
var d = q.getDay();
var y = q.getFullYear();

var date = new Date(y,m,d);

mydate=new Date('2011-04-11');
console.log(date);
console.log(mydate)

if(date>mydate)
{
    alert("greater");
}
else
{
    alert("smaller")
}
Muleskinner
  • 14,150
  • 19
  • 58
  • 79
Arepalli Praveenkumar
  • 1,247
  • 1
  • 13
  • 27
  • 3
    Shouldn't it be var d = q.getDate(); to get the day of the month, what you have gets the day of the week I think. – johnsnails Apr 24 '20 at 04:47
15

You can use a simple comparison operator to see if a date is greater than another:

var today = new Date();
var jun3 = new Date("2016-06-03 0:00:00");

if(today > jun3){
    // True if today is on or after June 3rd 2016
}else{
    // Today is before June 3rd
}

The reason why I added 0:00:00 to the second variable is because without it, it'll compare to UTC (Greenwich) time, which may give you undesired results. If you set the time to 0, then it'll compare to the user's local midnight.

M -
  • 26,908
  • 11
  • 49
  • 81
6

Using Javascript Date object will be easier for you. But as the Date object does not supports your format i think you have to parse your input string(eg: 25-02-2013) with '-' to get date month and year and then use Date object for comparison.

var x ='23-5-2010';
var a = x.split('-');
 var date = new Date (a[2], a[1] - 1,a[0]);//using a[1]-1 since Date object has month from 0-11
 var Today = new Date();
 if (date > Today)
    alert("great");
 else
    alert("less");
999k
  • 6,257
  • 2
  • 29
  • 32
  • Line 3 should be `var date = new Date (a[0], a[1] - 1,a[2]);` – k29 Jul 15 '15 at 16:10
  • @k29 No. Date Object accepts arguments in year, month, day order. With OP's input string a[2] is year so my statement is correct. Please check – 999k Jul 21 '15 at 06:26
  • Sorry you're right. I think I copied one of the strings from the other answers when trying out the code. – k29 Jul 21 '15 at 19:40
0

If your date input is in the format "25-02-2013", you can split the string into DD, MM and YYYY using the split() method:

var date_string="25-02-2013";

var day  = parseInt(date_string.split("-")[0]);
var month= parseInt(date_string.split("-")[1]);
var year = parseInt(date_string.split("-")[2]);

The parseInt() function is used to make the string into an integer. The 3 variables can then be compared against properties of the Date() object.

TheTurkey
  • 198
  • 9
0

The most significant points which needs to be remembered while doing date comparison

  • Both the dates should be in same format to get accurate result.
  • If you are using date time format and only wants to do date comparison then make sure you convert it in related format.

Here is the code which I used.

           var dateNotifStr = oRecord.getData("dateNotif");
           var today = new Date();       
           var todayDateFormatted = new Date(today.getFullYear(),today.getMonth(),today.getDate());

           var dateNotif=new Date(dateNotifStr);
           var dateNotifFormatted = new Date(dateNotif.getFullYear(),dateNotif.getMonth(),dateNotif.getDate());

Well, this can be optimized further but this should give you clear idea on what is required to make dates in uniform format.

mitpatoliya
  • 2,037
  • 12
  • 23
0

Here's my solution, getDay() doesn't work like some people said because it grabs the day of the week and not the day of the month. So instead you should use getDate like I used below

    var date = new Date();
    var m = date.getMonth();
    var d = date.getDate();
    var y = date.getFullYear();

    var todaysDate = formateDate(new Date(y,m,d));
    
    console.log("Todays date is: " + todaysDate)

    const formateDate = (assignmentDate) => {
        const date = new Date(assignmentDate)
        const formattedDate = date.toLocaleDateString("en-GB", {
            day: "numeric",
            month: "long",
            year: "numeric"
        })

        return formattedDate

    }

The function below is just to format the date into a legible format I could display to my users

0
<script type="text/javascript">
   // If you set the timezone then your condition will work properly, 
   // otherwise there is a possibility of error, 
   // because timezone is a important part of date function
    var todayDate = new Date().toLocaleString([], { timeZone: "Asia/Dhaka" }); //Today Date    
    var targetDate = new Date('2022-11-24').toLocaleString([], { timeZone: "Asia/Dhaka" });  
        
    console.log('todayDate ==', todayDate);  // todayDate == 10/31/2022, 12:15:08 PM
    console.log('targetDate ==', targetDate); // targetDate == 11/24/2022, 6:00:00 AM
    
    if(targetDate >= todayDate)
    {
        console.log("Today's date is small");
    }
    else
    {
        console.log("Today's date is big")
    }
</script>
0

This does the trick and also takes care of different timezones. Z is UTC Zero time.

if(new Date() > new Date('2023-08-22T12:46:00Z')){
Hebe
  • 661
  • 1
  • 7
  • 13