Search code examples
javascriptvue.jsvue-componentvue-clivuejs3

How can I get the hours for now in a time property?


How can I get now hours, for time property messagesList in VueCLI? When i clicked button, this property is filled as time now. time property will be decimal part. Example : (23.14)

Data is there :

 messagesList : [
            {
              id : 1,
              content : "Benim mesajım",
              opposite : false,
              me : true,  
              time : 23.13,   
            },
             {
              id : 2,
              content : "Karşı tarafın mesajı",
              opposite : true,
              me : false,  
              time : 23.13,   
            }
          ],

Code is there :

this.messagesList.push(
    {
        id : this.messagesList.length + 1,
        content : this.inputText,
        opposite : false,
        me : true,
        time : ?,
    }

Solution

  • Just create a function to return the current time in the format you want and call it when adding in the message.

    Here is the function:

    function getTime() {
        return parseFloat(`${new Date().getHours()}.${new Date().getMinutes()}`)
    }
    

    Here is the push to messageList:

    this.messagesList.push({
        id: this.messagesList.length + 1,
        content: this.inputText,
        opposite: false,
        me: true,
        time : getTime(),
    }