user1765862 user1765862 - 4 years ago 160
Javascript Question

datetime picker, check is today

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?

Answer Source

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);

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download