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?

2

6 Answers

Union Types

You can use a union type when declaring variables.

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

Template literals (ES6+)

Alternatively you can create a new variable and 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.

2

if you want to work with date, you can use momentjs module:

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. Declare all your variables with let and convert them all to strings and you should be good to go.

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:

1

You can use it like this. angular environment.ts

const URL = ""; const PORT = "8080"; const API_version = "/api/v1/"; export const environment = { production: false, BASE_URL: `${URL}:${PORT}${API_version}` }; 

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 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; 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy