How to concat string and number in Typescript

Union Types

You can use a union type when declaring variables.

let month: string | number = currentTime.getMonth();

if (month < 10) {
  month="0" + month;
}

Template literals (ES6+)

Alternatively you can create a new variable and use a template literal

const paddedMonth: string = `0${month}`;

Your string concatenation then turns into this for example:

str = `${year}-${paddedMonth}-${day} ${hours}:${minutes}:${seconds} `;

Much more readable, IMO.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)