How to concatenate string and number in Typescript

I am using method to get data

function date() {
    let str = '';

    const currentTime = new Date();
    const year = currentTime.getFullYear();
    const month = currentTime.getMonth();
    const day = currentTime.getDate();

    const hours = currentTime.getHours();
    let minutes = currentTime.getMinutes();
    let seconds = currentTime.getSeconds();
    if (month < 10) {
        //month = '0' + month;
    }
    if (minutes < 10) {
        //minutes = '0' + minutes;
    }
    if (seconds < 10) {
        //seconds = '0' + seconds;
    }
    str += year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds + ' ';

    console.log(str);
}

      

And as an output, I get

2017-6-13 20:36:6 

      

I would like to get the same, but how

2017-06-13 20:36:06 

      

But if I try one of the lines I commented out, for example this

month = '0' + month;

      

I get an error

Argument of type 'string' is not assignable to parameter of type 'number'.

      

How can I concatenate string and number?

+10


source to share


5 answers


Template literals (ES6 +)

Instead of concatenation like month = '0' + month;

you can use literal pattern

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.

Union types

You can also use the union type when declaring variables.

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

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

      

+28


source


if you want to work with date you can use momentjs module: https://momentjs.com

moment().format('MMMM Do YYYY, h:mm:ss a'); // July 13th 2017, 11:18:05 pm
moment().format('dddd');                    // Thursday
moment().format("MMM Do YY");               // Jul 13th 17
moment().format('YYYY [escaped] YYYY');     // 2017 escaped 2017
moment().format();                          // 2017-07-13T23:18:05+04:30

      



and about the error you got you most use this:

 let monthStr: string = month;
 if ( month < 10) {
     monthStr = '0' + month;
 }

      

+4


source


First of all, I'm not sure why you define month

how const

and then try to change it. Declare all your variables with let

and convert them all to strings and you should be good to go.

function date() {
    let str = '';

    const currentTime = new Date();
    let year = currentTime.getFullYear().toString();
    let month = currentTime.getMonth().toString();
    let day = currentTime.getDate().toString();

    let hours = currentTime.getHours().toString();
    let minutes = currentTime.getMinutes().toString();
    let seconds = currentTime.getSeconds().toString();
    if (month < 10) {
        month = '0' + month;
    }
    if (minutes < 10) {
        minutes = '0' + minutes;
    }
    if (seconds < 10) {
        seconds = '0' + seconds;
    }
    str += year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds + ' ';

    console.log(str);
}

      

See how it works here: https://jsfiddle.net/40jbg8qt/

0


source


You can use something like this:

let pais:string = 'Ecuador';
let codigo:number = 593;
let opcionUno:string = this.pais + this.number
let opcionDos:string = this.pais.concat(this.number);

      

0


source


You can also do something like this:

let month: string | number = currentTime.getMonth();
if (month < 10) month = '0' + month;

      

Or that:

const month = currentTime.getMonth();
const monthStr = (month < 10 ? "0" : "") + month;

      

-1


source







All Articles