Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在Typescript中连接字符串和数字_Javascript_Typescript_Concat - Fatal编程技术网

Javascript 如何在Typescript中连接字符串和数字

Javascript 如何在Typescript中连接字符串和数字,javascript,typescript,concat,Javascript,Typescript,Concat,我正在使用这个方法来获取数据 function date() { let str = ''; const currentTime = new Date(); const year = currentTime.getFullYear(); const month = currentTime.getMonth(); const day = currentTime.getDate(); const hours = currentTime.getHou

我正在使用这个方法来获取数据

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);
}
我想得到同样的东西,但是

2017-06-13 20:36:06 
但是如果我尝试其中一行,我注释掉了,例如这一行

month = '0' + month;
我犯了一个错误

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

我如何处理字符串和数字?

模板文本(ES6+)

而不是像
month='0'+month这样的串联
你可以使用

然后,字符串连接会变成以下形式,例如:

str = `${year}-${paddedMonth}-${day} ${hours}:${minutes}:${seconds} `;
更具可读性,国际海事组织

联合类型

在声明变量时,也可以使用

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

if (month < 10) {
  month = '0' + month;
}
let month:string | number=currentTime.getMonth();
如果(月<10){
月份='0'+月份;
}

首先,我不知道您为什么要将
月份定义为
常量,然后尝试更改它。用
let
声明所有变量,并将它们全部转换为字符串,这样就可以开始了

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);
}
函数日期(){
设str='';
const currentTime=新日期();
让year=currentTime.getFullYear().toString();
let month=currentTime.getMonth().toString();
let day=currentTime.getDate().toString();
让小时数=currentTime.getHours().toString();
让分钟数=currentTime.getMinutes().toString();
设秒数=currentTime.getSeconds().toString();
如果(月<10){
月份='0'+月份;
}
如果(分钟<10){
分钟='0'+分钟;
}
如果(秒<10){
秒='0'+秒;
}
str+=年+'-'+月+'-'+日+'+小时+':'+分钟+':'+秒+';
console.log(str);
}

查看此处的工作情况:

如果您想使用date,可以使用momentjs模块:

关于你所犯的错误,你最常用的方法是:

 let monthStr: string = month;
 if ( month < 10) {
     monthStr = '0' + month;
 }
let month: string | number = currentTime.getMonth();
if (month < 10) month = '0' + month;
let pais:string = 'Ecuador';
let codigo:number = 593;
let opcionUno:string = this.pais + this.number
let opcionDos:string = this.pais.concat(this.number);
let monthStr:string=month;
如果(月<10){
monthStr='0'+月;
}

您也可以这样做:

 let monthStr: string = month;
 if ( month < 10) {
     monthStr = '0' + month;
 }
let month: string | number = currentTime.getMonth();
if (month < 10) month = '0' + month;
let pais:string = 'Ecuador';
let codigo:number = 593;
let opcionUno:string = this.pais + this.number
let opcionDos:string = this.pais.concat(this.number);
let month:string | number=currentTime.getMonth();
如果(月<10)月='0'月+月;
或者这个:

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

您可以使用以下内容:

 let monthStr: string = month;
 if ( month < 10) {
     monthStr = '0' + month;
 }
let month: string | number = currentTime.getMonth();
if (month < 10) month = '0' + month;
let pais:string = 'Ecuador';
let codigo:number = 593;
let opcionUno:string = this.pais + this.number
let opcionDos:string = this.pais.concat(this.number);

使用不同类型的变量
string
?另请参见:这是一个好主意-除非OP将此推送到web并希望支持IE:如果正在使用TypeScript,为非es6编译时,它会将模板文本转换为常规字符串连接。使用
let
时,能否将
month
的类型从number更改为typescript中的字符串?