簡體   English   中英

如何在Typescript中連接字符串和數字

[英]How to concat string and number in Typescript

我正在使用方法來獲取數據

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'.

我怎么能連接字符串和數字?

模板文字 (ES6 +)

而不是像month = '0' + month;那樣的連接month = '0' + month; 您可以使用模板文字

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

然后你的字符串連接變成了這個例子:

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

更具可讀性,IMO。

聯盟類型

在聲明變量時也可以使用union類型

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

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

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

首先,我不確定為什么你將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 pais:string = 'Ecuador';
let codigo:number = 593;
let opcionUno:string = this.pais + this.number
let opcionDos:string = this.pais.concat(this.number);

你可以像這樣使用它。 角度環境.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}`
};

你也可以這樣做:

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

或這個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM