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?
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?
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);
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));
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>
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/
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();
}
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.
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');
}