【问题标题】:How to concat string and number in Typescript如何在 Typescript 中连接字符串和数字
【发布时间】:2017-12-18 17:06:18
【问题描述】:

我正在使用方法获取数据

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-6-13 20:36:6 

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

2017-06-13 20:36:06 

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

month = '0' + month;

我收到错误

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

如何连接字符串和数字?

【问题讨论】:

标签: javascript typescript concat


【解决方案1】:

你可以这样使用它。 角度环境.ts

const URL = "http://127.0.0.1";
const PORT = "8080";
const API_version = "/api/v1/";

export const environment = {
    production: false,
    BASE_URL: `${URL}:${PORT}${API_version}`
};

【讨论】:

    【解决方案2】:

    联合类型

    您可以在声明变量时使用union type

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

    模板字面量 (ES6+)

    或者,您可以创建一个新变量并使用 template literal

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

    你的字符串连接然后变成这个例子:

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

    更具可读性,IMO。

    【讨论】:

    • 这是个好主意 - 除非 OP 将其推送到网络并希望支持 IE:caniuse.com/#search=template%20literals
    • 如果正在使用 TypeScript,它会在为非 es6 编译时将模板文字转换为常规字符串连接。
    【解决方案3】:

    你可以这样使用:

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

    【讨论】:

      【解决方案4】:

      你也可以这样做:

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

      或者这个:

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

      【讨论】:

        【解决方案5】:

        首先,我不确定您为什么将month 定义为const,然后尝试更改它。使用 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);
        }
        

        在这里查看它的工作原理:https://jsfiddle.net/40jbg8qt/

        【讨论】:

        • 当您使用let 时,您能否将month 的类型从打字稿中的数字更改为字符串?
        【解决方案6】:

        如果你想使用日期,你可以使用 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
        

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-11-30
          • 1970-01-01
          • 2016-07-18
          • 1970-01-01
          • 2011-10-22
          • 2016-01-09
          相关资源
          最近更新 更多