1

I'm fetching users input in jquery datetime picker in format like this

2017-02-07 10:05

which is fine. My question is: how can I check is this users input today or not?

user1765862
  • 13,635
  • 28
  • 115
  • 220

7 Answers7

2

After getting suggestion from this question this is how you can do that. First setting the date constructor for today and the test date then set their time portion equal to zero and compare.

There are also other cool plugins to achieve the same more easily and more accurately like dateJs , momentJs

Using momentJs

var inputDate = new Date("2017-02-07 10:05");

var isToday = inputDate.isSame(new Date(), "day");

Using dateJs

var isToday = Date.equals(Date.today(), new Date("2017-02-07 10:05").clearTime());

Using native JavaScript date

var inputDate = new Date("2017-02-07 10:05");

var todaysDate = new Date();

var isToday = (inputDate.setHours(0, 0, 0, 0) == todaysDate.setHours(0, 0, 0, 0));

console.log(isToday);
Community
  • 1
  • 1
Parvez Rahaman
  • 4,269
  • 3
  • 22
  • 39
1

Simplest Way...

var date = new Date('2017-02-07 10:05');
var now = new Date();
console.log(now.toString().substring(4, 15) == date.toString().substring(4, 15));
1

How about something like this:

<html>
<head>
</head>
<body>
<script>
    var dateString = "2017-02-09 10:05";
    var dateObj = new Date(dateString);
    var ddSomeDate = dateObj.getDate();
    var mmSomeDate = dateObj.getMonth()+1; //January is 0!
    var yyyySomeDate = dateObj.getFullYear();

    var dateObjToday = new Date();
    var ddCurrentDate = dateObjToday.getDate();
    var mmCurrentDate = dateObjToday.getMonth()+1; //January is 0!
    var yyyyCurrentDate = dateObjToday.getFullYear();

    if (ddSomeDate == ddCurrentDate &&
        mmSomeDate == mmCurrentDate &&
        yyyySomeDate == yyyyCurrentDate)
        console.log("Same day");
    else
        console.log("NOT same day");

</script>
</body>
</html>
1

Try this is used to check the date is today or not

Here is Jquery code

 $('#dp').datepicker({
onSelect: function(dateText) {
    var today = new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate()).getTime();
    var selected = new Date(dateText).getTime();
    if (today > selected) alert('prior to today');
    else if (today < selected) alert('after today');
    else alert('today');
}
});​

And My html code

  <input id="dp"/>

Demo here http://jsfiddle.net/j08691/yBDVJ/

Abi
  • 724
  • 6
  • 22
0

You must parse the date and use this isDateToday function to check:

var date = new Date('2017-02-07 10:05');

var isToday = isDateToday(date);

function isDateToday(td){
    var d = new Date();
    return td.getDate() == d.getDate() && td.getMonth() == d.getMonth() && td.getFullYear() == d.getFullYear();
}
Constantin Galbenu
  • 16,951
  • 3
  • 38
  • 54
0

you can get the current Date-Month-year by:

var d = new Date();
var date = d.getDate();
var month = d.getMonth();
var year = d.getFullYear();

then match the user selected date, month and year by these values.

Zainub Wahid
  • 251
  • 2
  • 6
-1

Try something like this:

    var today = new Date();
  today.setHours(0);
  today.setMinutes(0);
  today.setSeconds(0);
  if (Date.parse(today) == Date.parse(selectedDate)) {
    alert('today!');
  } else {
    alert('not today');
  }
Hadee
  • 1,392
  • 1
  • 14
  • 25