0

Started using javascript and been given a task to create a getter method that calculates and returns the number of years that the customer has been a customer.

However I don't know how store the 'customers' start date in the getter method to begin with. Then after that I'm not sure how to convert the difference of those dates into just years.

Any help would be grateful!

Heres piece of code I'm working on:

  class Customer extends Person{
constructor(id_number, first_name, last_name, email_address, customer_start_date){
super(id_number, first_name, last_name);
this.email_address = email_address;
this.customer_start_date = customer_start_date;



  }
  get email_address(){
    return this.email_address;
  }
  get customer_start_date(){
    customer_start_date = new Date(2018, 11, 22);
    return this.customer_start_date;
  }

}

let s2 = new Staff(123577, "Steve", "Smith", "stevesmith@work.com", (2018, 11, 22));
console.log(s2.first_name , "has been a customer for this many years: ", s2.customer_start_date);
  • Not a full answer, but when date manipulation is required, I start to consider using momentjs https://momentjs.com/ which has functions for what you just asked for and great documentation and tons of help online. – Trevor Jan 15 '20 at 18:28
  • 2
    Can you not just use the default Javascript Date class subtraction features? https://stackoverflow.com/questions/41948/how-do-i-get-the-difference-between-two-dates-in-javascript To convert milliseconds to years, just use the number of milliseconds in a year, which is 31536000000. Ex. var diffInMilliseconds = new Date() - s2.customer_start_date; var diffInYears = diffInMilliseconds / 31536000000; That said, this doesn't take into account tricky things like timezones, so I still also endorse moment.js. – Declan McKelvey-Hembree Jan 15 '20 at 18:47
  • Would i add this 'var diffInMilliseconds....' in the get method? What you describe seems across the right lines Also i wouldn't have to worry about the timezones . I believe its neant to be a simple year output(not too specific). – Bryan Seisay Jan 15 '20 at 18:53
  • This really is a duplicate of [*Difference between two dates in years, months, days in JavaScript*](https://stackoverflow.com/questions/17732897/difference-between-two-dates-in-years-months-days-in-javascript), the rest is opinion based. – RobG Jan 16 '20 at 07:25
  • I'll have a look at that post then – Bryan Seisay Jan 16 '20 at 12:29

1 Answers1

0

The start date can be stored as a property of a customer. The value might be a string like "2016-07-23" or a time value like 1469232000000 or whatever best suits the backend storage. I'd be tempted to do everything as UTC to avoid timezone and DST confusion.

Note that the built–in parser should treat "2016-07-23" as UTC, see Why does Date.parse give incorrect results?

The following is a simple example to show the concept.

class Person {
  constructor (id, firstName, lastName) {
    this.id = id;
    this.firstName = firstName;
    this.lastName = lastName;
  }
}

class Customer extends Person {
  constructor (id, firstName, lastName, startDate) {
    super(id, firstName, lastName);
    this.startDate = startDate;
  }
  
  // Simplistic example, production function should be much more sophisticated
  get customerYears() {
    return Math.round((Date.now() - new Date(this.startDate)) / (365.25 * 8.64e7));
  }
}

let p = new Customer(0, 'Pete', 'Smith', '2017-01-16');

console.log(`${p.firstName} has been a customer since ${p.startDate}, which is about ${p.customerYears} years.`);

The customerYears method is intentionally simplistic, you should use something that suits from Difference between two dates in years, months, days in JavaScript or a library that includes date arithmetic functions.

RobG
  • 142,382
  • 31
  • 172
  • 209
  • Thanks! I'm starting to understand this more due to this. I've got a question though; What's '(`${' doing? – Bryan Seisay Jan 17 '20 at 12:18
  • @BryanSeisay—it's the syntax for a [*template literal*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals), which allows embedding expressions in strings so they print with the resolved expression value. – RobG Jan 17 '20 at 22:16