0

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
Anjana
  • 366
  • 5
  • 21
  • Does this answer your question? [Compare two dates with JavaScript](https://stackoverflow.com/questions/492994/compare-two-dates-with-javascript) – R. Richards Jun 09 '20 at 12:54
  • Create a date instance based on the output of your datepickers and compare them using Date.getTime() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date –  Jun 09 '20 at 12:56

3 Answers3

1

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

anonymous
  • 46
  • 2
  • 5
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))
mr. pc_coder
  • 16,412
  • 3
  • 32
  • 54
0

Here you got the methods of Date object.

By using todayTime.getDate()you will have a day between 0 and 31 so then with all the methods provided by the object you can get month, year, hours, minutes and seconds and just compare them to your json object.

Alexis
  • 1,685
  • 1
  • 12
  • 30