【问题标题】:How can I get the hours for now in a time property?我怎样才能在时间属性中获得现在的时间?
【发布时间】:2021-03-28 08:45:59
【问题描述】:

对于 VueCLI 中的时间属性消息列表,我如何获取现在的小时数?当我单击按钮时,此属性现在按时间填充。时间属性将是小数部分。示例:(23.14)

有数据:

 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,   
            }
          ],

代码在那里:

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

【问题讨论】:

  • 这个时间格式是什么?整数部分是小时,小数部分是分钟?
  • 它将是小数部分。示例:23.14
  • 我不确定我是否理解,这会做你想要的吗? (不要按原样使用,太丑了,我就是想看看是不是你想要的) time: (d => parseFloat(d.getHours() + '.' + ('0' + d.getMinutes()).slice(-2)))(new Date) 注意:如果是,那你可能想换格式,很不常规,也不是很方便(怎么知道是上周发的还是今天发的?)
  • 我是这个平台的新手。对不起。我有一个messageList 数组。 messagesList 按时间有一个属性。 time 属性应该采用当前的小时和分钟。我在哪里可以定义当前时间*以及当前时间如何填充时间属性?
  • 此应用未连接此数据库。仅从数组中获取变量以进行显示...

标签: javascript vue.js vue-component vue-cli vuejs3


【解决方案1】:

只需创建一个函数,以您想要的格式返回当前时间,并在添加消息时调用它。

函数如下:

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

这里是消息列表的推送:

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

【讨论】:

    猜你喜欢
    • 2016-02-13
    • 1970-01-01
    • 2019-11-02
    • 1970-01-01
    • 2012-12-07
    • 1970-01-01
    • 2023-02-05
    • 2013-08-07
    • 1970-01-01
    相关资源
    最近更新 更多