0

I am getting date in string format from API. End Date 2014-06-03T06:16:52. I need to write an if-else logic and compare the end date and current date.If end date is less than current date then show customer as In Active and if end date is greater than display the Customer as Active. I have tried following logic but I am not able to understand and get today's time in string fromat.

  this.endDate = this.sampleData != null ? 
  this.sampleData.customerStartDate : null;
  this.currentDate = new Date();
  var dd = this.currentDate.getDate();
  var mm = this.currentDate.getMonth() + 1;
  var yyyy = this.currentDate.getFullYear();
  this.currentDate = new Date().toLocaleString()
  console.log('End Date', this.endDate);
  console.log('Current Date: ', this.currentDate);
  if (this.endDate == null) {
    this.customerStatus = 'Active';
  } else {
    this.customerStatus = 'In Active';
  }

I am getting current date as Current Date: 4/2/2019, 1:23:34 AM I want to be able to get in same format as End Date. My main task is to compare the dates how do I achieve it ?

Cpt.Kitkat
  • 120
  • 4
  • 17
  • [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Ivan Rubinson Apr 01 '19 at 20:03
  • 2
    Duplicate to https://stackoverflow.com/questions/492994/compare-two-dates-with-javascript – Bojoer Apr 01 '19 at 20:11
  • 2
    Possible duplicate of [Compare two dates with JavaScript](https://stackoverflow.com/questions/492994/compare-two-dates-with-javascript) – R. Richards Apr 01 '19 at 20:12

4 Answers4

1

Ideally you want to clean up the date you're getting from an API, and convert it to a JS Date object. You can do this by keeping only the 2014-06-03T06:16:52 part, and giving it to the new Date() constructor.

You can get the current date by calling new Date() without parameters.

You can the turn the dates in to numbers by calling getTime() on each.

You can then compare the numbers.

const incoming_date = new Date('2014-06-03T06:16:52');
const current_date = new Date();
if (incoming_date.getTime() < current_date.getTime() {
    // incoming_date is before current_date
} else {
    // current_date is before incoming_date
}
Ivan Rubinson
  • 3,001
  • 4
  • 19
  • 48
  • I am getting this error core.js:12584 ERROR TypeError: _this.endDate.getTime is not a function in my typescript code – Cpt.Kitkat Apr 01 '19 at 20:15
  • That's because you need to convert `endDate` to a standard valid date first. – Ivan Rubinson Apr 01 '19 at 20:19
  • Thank you so much, Ivan Rubinson. I got it working with the help of Moment Library using Aaron Ross's answer initially but like you said we don't need to import heavy dependency so used your method. – Cpt.Kitkat Apr 01 '19 at 20:37
0

as simple as this:

let date=new Date("2014-06-03T06:16:52")

date>new Date()
jonathan Heindl
  • 844
  • 7
  • 16
  • Is the `>` operator overloaded in JS? I think you need to convert the `Date` object in to a number first. – Ivan Rubinson Apr 01 '19 at 20:07
  • I was wondering so as well after I saw you post so I double checked with a few variations ans it seems to work - as far as I know you can "implement" a greater /smaller if you provide a .valueOf() for an object (the valueOf() method of date returns millseconds since epoch so its accurate) – jonathan Heindl Apr 01 '19 at 20:11
  • How is it in terms of browser backwards compatibility? – Ivan Rubinson Apr 01 '19 at 20:12
  • https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Object/valueOf browser compatibility on the bottom (I asume the numbers mean since version oO) – jonathan Heindl Apr 01 '19 at 20:16
0

I personally like to use moment() for javascript dates. You really just need to have it compare the same format, so you could have something like:

this.currentDate = moment().toISOString();
const dataDate = this.sampleData ? this.sampleData.customerStartDate : null;
this.endDate = moment(dataDate).toISOString();
if (this.endDate > this.currentDate) {
    this.customerStatus = 'Active';
} else {
    this.customerStatus = 'Inactive';
}
Ivan Rubinson
  • 3,001
  • 4
  • 19
  • 48
Aaron Ross
  • 141
  • 1
  • 5
  • 1
    moment is a large library. There's no reason to bring in such a heavy dependency simply for comparing two dates. It's also monolithic, meaning it can't be made any leaner. – Ivan Rubinson Apr 01 '19 at 20:11
0

you could try to express dates in ms since the Unix Epoch with getTime() and compare them

if (currDate.getTime() > endDate.getTime()) {
    // set customer to inactive
} else {
    // keep customer active
}
hpc
  • 41
  • 4