[英]How to concat 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 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?我怎么能连接字符串和数字?
Template literals (ES6+) 模板文字 (ES6 +)
Instead of concatenation like month = '0' + month;
而不是像
month = '0' + month;
那样的连接month = '0' + month;
You can use a template literal 您可以使用模板文字
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 type when declaring variables. 在声明变量时也可以使用union类型 。
let month: string | number = currentTime.getMonth();
if (month < 10) {
month = '0' + month;
}
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;
}
First of all, I'm not sure why you are defining month
as a const
and then trying to change it. 首先,我不确定为什么你将
month
定义为const
然后尝试改变它。 Declare all your variables with let
and convert them all to strings and you should be good to go. 使用
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/
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);
You can use it like this.你可以像这样使用它。 angular environment.ts
角度环境.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}`
};
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.