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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45088700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:54:20  来源:igfitidea点击:

How to concat string and number in Typescript

javascripttypescriptconcat

提问by Anna F

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 output I get

作为输出我得到

2017-6-13 20:36:6 

I would like to get the same thing, but like

我想得到同样的东西,但喜欢

2017-06-13 20:36:06 

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

但是如果我尝试其中一行,我注释掉了,例如这一行

month = '0' + month;

I get error

我得到错误

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

How could I concat string and number?

我怎么能连接字符串和数字?

回答by Tyler Miller

Template literals(ES6+)

模板字面量(ES6+)

Instead of concatenation like month = '0' + month;You can use a template literal

而不是像 month = '0' + month;你可以使用模板文字的连接

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.

更具可读性,IMO。

Union Types

联合类型

You can also use a union typewhen declaring variables.

您还可以在声明变量时使用联合类型

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

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

回答by Ali Ebrahimi

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

如果你想使用日期,你可以使用 momentjs 模块: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 like this:

关于你得到的错误,你最常用的是这样的:

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

回答by abagshaw

First of all, I'm not sure why you are defining monthas a constand then trying to change it. Declare all your variables with letand convert them all to strings and you should be good to go.

首先,我不确定你为什么定义month为 aconst然后试图改变它。声明所有变量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);
}

See it working here: https://jsfiddle.net/40jbg8qt/

看到它在这里工作:https: //jsfiddle.net/40jbg8qt/

回答by martosfre

You could 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);

回答by libertyernie

You could also do something like this:

你也可以做这样的事情:

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

Or this:

或这个:

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