How to compare date and time in Angular
I'm trying to compare current date and time with ngb date and time picker which is in JSON format:
var todayTime = new Date();
Output:
Tue Jun 09 2020 17:43:30 GMT+0530 (India Standard Time)
I want to compare this with the below date and time which is in JSON format and check if entered date and time are greater than the current date and time.
Output for ngb timepicker
hour: 13
minute: 38
second: 22
Output for ngb datepicker
day: 9
month: 6
year: 2020
How can I achieve this?
Solution 1:
var date = new Date();
var objDate= { day: 9,month: 6, year: 2020}
var objMinute={ hour: 13,minute: 38,second: 22}
function compareDays(dateObj,hour){
var objDate=new Date(dateObj.year+'-'+dateObj.month+"-"+dateObj.day+
" "+ hour.hour +":" + hour.minute + ":" + hour.second + ".000Z");
console.log(objDate);
console.log(date);
return (date.getTime() / 1000) > (objDate.getTime() / 1000) ? true :false;
}
console.log(compareDays(objDate,objMinute))
Solution 2:
You can convert your date and time object to date with
new Date("month day year hour:min:sec")
then check with current time stamp