47

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 6

97

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
  • That's a good idea - unless the OP is pushing this out to the web and wants to support IE: caniuse.com/#search=template%20literals
    – abagshaw
    Jul 13, 2017 at 19:31
  • 6
    If TypeScript is being used, it will convert template literals to regular string concatenation when compiling for non-es6. Jul 14, 2017 at 14:13
7

if you want to work with date, you can use momentjs module: 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;
 }
4

You can use it like this. angular environment.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}`
};
3

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: https://jsfiddle.net/40jbg8qt/

1
  • can you change type of month from number to string in typescript when you use let?
    – Aprillion
    Sep 22, 2017 at 16:55
1

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

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

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.