0

I am building an AngularJS/Rails web app, part of it is creating bookings (function bookings) and in the dashboard I am trying to display two different tabs one with Current Bookings and one with Previous Bookings. the booking model has a function_date attribute and I am retrieving it from the API into a $scope.bookings array.

How to I compare dates (run an if statement on it) to do if function_data > today's date store it into CurrentBookings array if not store it into PreviousBookings array?

Hope that makes sense.

P.S. I am still teaching myself how to program so it might be a stupid question for some.

  • possible duplicate http://stackoverflow.com/questions/26435313/angularjs-ng-if-comparing-dates – Brij Raj Singh - MSFT Apr 10 '16 at 03:21
  • The issue I am having is date formatting, if I do as per the suggested article it doesn't work, as new Date(); produces 2016-04-10T03:35:00.128Z and my function_date comes in YYYY-MM-DD format – Anthony Indraus Apr 10 '16 at 03:36
  • You can format new Date() to YYYY-MM-DD then the comparison will work. Look at this question for ways to format JS dates http://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date. Also I recommend moment.js library but it may be overkill. – carlosdubusm Apr 10 '16 at 03:41

2 Answers2

0

many way solved this problem but i am using to convert time in milliseconds then easy to compare.

var n = Date.now();

its give the current Return the number of milliseconds since 1970/01/01:

1460260009358
Anurag Pandey
  • 704
  • 1
  • 6
  • 14
0

First, try converting your string into a date. Then compare it to now.

var date = new Date('2030-03-30'); //this is a Date object

if (date > new Date()) { //Date object comparison
   //[...]
}
// or
if (date.getTime() > Date.now()) { //unix timestamp (integer) comparison
   //[...]
}
jlowcs
  • 1,905
  • 1
  • 7
  • 15