Ørnulf Arntsen
Ørnulf Arntsen

Reputation: 193

Es6 class variables

I've made this ES6 class, and I'm trying to use the variables I defined in the constructor, but when I console.log this.customerTz it returns undefined.

I've also tried to put variables at the top of the class, but it doesn't seem to work.
Is it possible to have variables?

class DateTimeConverter {
constructor() {
    this.format = 'YYYY-MM-DD HH:mm:ss';
    this.customerTz = 'Europe/Oslo';
}

static convertToUtc(date) {
    console.log(this.customerTz);
    // Set customer timezone
    date = moment.tz(date, this.customerTz);

    // Convert to UTC
    date = date.clone().tz('UTC');

    // Set format to something PHP thinks is valid
    date = date.format(this.format);

    return date;
}
}

Upvotes: 0

Views: 262

Answers (4)

Abhishek
Abhishek

Reputation: 1487

convertToUtc is a static method and it can't access any property on instance. Instead you can modify the code like below

class DateTimeConverter {
    
    static convertToUtc(date) {
        console.log(DateTimeConverter.customerTz); // Static property
        
        // Set customer timezone
        date = moment.tz(date, DateTimeConverter.customerTz);
    
        // Convert to UTC
        date = date.clone().tz('UTC');
    
        // Set format to something PHP thinks is valid
        date = date.format(DateTimeConverter.format);
    
        return date;
    }
}

DateTimeConverter.format = 'YYYY-MM-DD HH:mm:ss';
DateTimeConverter.customerTz = 'Europe/Oslo';


console.log(DateTimeConverter.convertToUtc(new Date()));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.0/moment.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.11/moment-timezone.min.js"></script>

Upvotes: 2

reddberckley
reddberckley

Reputation: 68

Your function convertToUtc is static. Static functions are associated with the class itself and not instances of the class. The constructor is not called because you don't create an instance. so this.customerTz will not be defined inside the function. take out the static

class DateTimeConverter {
  constructor() {
   this.format = 'YYYY-MM-DD HH:mm:ss';
   this.customerTz = 'Europe/Oslo';
  }

  convertToUtc(date) {
   console.log(this.customerTz);

   date = moment.tz(date, this.customerTz);

   // Convert to UTC
   date = date.clone().tz('UTC');

   // Set format to something PHP thinks is valid
   date = date.format(this.format);

   return date;
  }
}

Upvotes: 0

Bergi
Bergi

Reputation: 665507

It looks like you do not want a class at all, as there is no reason to instantiate this constructor. Instead, use a plain object literal for your module:

const DateTimeConverter = {
    format: 'YYYY-MM-DD HH:mm:ss',
    customerTz: 'Europe/Oslo',
    convertToUtc(date) {
        console.log(this.customerTz);
        // Set customer timezone
        date = moment.tz(date, this.customerTz);

        // Convert to UTC
        date = date.clone().tz('UTC');

        // Set format to something PHP thinks is valid
        date = date.format(this.format);

        return date;
    }
};

Upvotes: 1

neddinn
neddinn

Reputation: 66

Since convertToUtc is a static method, you have no instance this of the DateTimeConverter class when you are calling convertToUtc method. You could make convertToUtc to not be a static method and then call it on an instance of the DateTimeConverter class.

Upvotes: 1

Related Questions