How to concatenate two numbers in javascript?

后端 未结 16 2338
傲寒
傲寒 2020-12-13 16:33

I\'d like for something like 5 + 6 to return \"56\" instead of 11.

16条回答
  •  有刺的猬
    2020-12-13 17:19

    I converted back to number like this..

    const timeNow = '' + 12 + 45;
    const openTime = parseInt(timeNow, 10);
    

    output 1245

    -- edit --

    sorry,

    for my use this still did not work for me after testing . I had to add the missing zero back in as it was being removed on numbers smaller than 10, my use is for letting code run at certain times May not be correct but it seems to work (so far).

    h = new Date().getHours();
    m = new Date().getMinutes();
    isOpen: boolean;
    
    timeNow = (this.m < 10) ? '' + this.h + 0 + this.m : '' + this.h + this.m;
    
    openTime = parseInt(this.timeNow);
    
    
    closed() {
    
    (this.openTime >= 1450 && this.openTime <= 1830) ? this.isOpen = true : 
    this.isOpen = false;
    (this.openTime >= 715 && this.openTime <= 915) ? this.isOpen = true : 
    this.isOpen = false;
    

    }

    The vote down was nice thank you :)

    I am new to this and come here to learn from you guys an explanation of why would of been nice.

    Anyways updated my code to show how i fixed my problem as this post helped me figure it out.

提交回复
热议问题