32

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?

abagshaw
  • 4,960
  • 3
  • 28
  • 58
Anna F
  • 1,295
  • 2
  • 13
  • 34

5 Answers5

67

Template literals (ES6+)

Instead of concatenation like 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.

Union Types

You can also use a union type when declaring variables.

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

if (month < 10) {
  month = '0' + month;
}
Tyler Miller
  • 896
  • 6
  • 18
  • That's a good idea - unless the OP is pushing this out to the web and wants to support IE: https://caniuse.com/#search=template%20literals – abagshaw Jul 13 '17 at 19:31
  • 5
    If TypeScript is being used, it will convert template literals to regular string concatenation when compiling for non-es6. – libertyernie Jul 14 '17 at 14:13
4

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

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/

abagshaw
  • 4,960
  • 3
  • 28
  • 58
0

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);
Avery
  • 2,035
  • 4
  • 33
  • 32
martosfre
  • 191
  • 2
  • 6
-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;
libertyernie
  • 2,312
  • 1
  • 10
  • 11