【问题标题】:How to show date correctly in react? [duplicate]如何在反应中正确显示日期? [复制]
【发布时间】:2019-03-19 10:41:53
【问题描述】:

我正在构建一个集成了 Solidity 合约的门户网站。在合同中,我已经从1539491531 这样的uint 中保存了日期。

当我在网页上显示它时,它看起来像这样:

它显示不同的日期。但是当我在代码中这样做时。

<Feed.Date>{Date(date)}</Feed.Date> 

所有日期都显示为现在的时间,如下所示:Mon Oct 15 2018 08:44:18 GMT+0530 (India Standard Time)

我该如何解决这个问题?有人可以帮我解决这个问题吗?谢谢。

【问题讨论】:

  • 不要发图片,发文字。不是每个人都能看到图像。

标签: javascript reactjs date


【解决方案1】:

您的问题是您缺少 new 关键字。

Date() // Mon Oct 15 2018 13:30:55 GMT+1000 (Australian Eastern Standard Time)
Date(1) // Mon Oct 15 2018 13:30:55 GMT+1000 (Australian Eastern Standard Time)
Date('something random') // Mon Oct 15 2018 13:30:55 GMT+1000 (Australian Eastern Standard Time)

但是,new:

new Date() // Mon Oct 15 2018 13:30:55 GMT+1000 (Australian Eastern Standard Time)
new Date(1) // Thu Jan 01 1970 10:00:00 GMT+1000 (Australian Eastern Standard Time)
new Date('something random') // Invalid Date

note on MDN

将其作为常规函数调用(即不使用 new 运算符)将返回字符串而不是 Date 对象

当您在没有new 的情况下调用Date() 时,它会调用一个返回当前时间字符串的函数。它不接受任何参数,但在 Javascript 中,你可以传递任何你喜欢的东西,它仍然可以工作,所以它只是忽略你传递的任何参数,并返回一个 string,而不是当前时间的日期。

typeof Date() // string
typeof new Date() // object

正如 edvilme 所指出的,您的时间戳也看起来以 为单位,而不是 new Date() 要求的毫秒 - 您只需乘以 1000 即可使其工作

new Date(1529491531) // Mon Jan 19 1970 02:51:31 GMT+1000 (Australian Eastern Standard Time)
new Date(1529491531 * 1000) // Wed Jun 20 2018 20:45:31 GMT+1000 (Australian Eastern Standard Time)

【讨论】:

    【解决方案2】:

    尝试像这样调整您的代码:

    <Feed.Date>{ new Date(date) }</Feed.Date> 
    

    通过创建日期对象(通过new),您可以简化从UTC 纪元时间(即date 的值)到人类可读日期字符串的转换。

    有关日期的更多信息,see this MDN article

    【讨论】:

      【解决方案3】:

      我认为您可以使用格式功能来帮助您。

      Date.prototype.format = function(format)
      {
       var o = {
       "M+" : this.getMonth()+1, //month
       "d+" : this.getDate(),    //day
       "h+" : this.getHours(),   //hour
       "m+" : this.getMinutes(), //minute
       "s+" : this.getSeconds(), //second
       "q+" : Math.floor((this.getMonth()+3)/3),  //quarter
       "S" : this.getMilliseconds() //millisecond
       }
       if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
       (this.getFullYear()+"").substr(4 - RegExp.$1.length));
       for(var k in o)if(new RegExp("("+ k +")").test(format))
       format = format.replace(RegExp.$1,
       RegExp.$1.length==1 ? o[k] :
       ("00"+ o[k]).substr((""+ o[k]).length));
       return format;
      }

      那么你可以这样使用它:

      var d1 = new Date();
      d1.format('yyyy-MM-dd');

      【讨论】:

        【解决方案4】:

        您错过了新日期之前的日期(日期) 另外,也许尝试将该值乘以 100

        <Feed.Date>{new Date(date*100)}</Feed.Date> 
        

        【讨论】:

        • *100是为了补秒,以防时间戳不完整。见stackoverflow.com/questions/847185/…
        • 它看起来像那些时间戳,因此您需要乘以 1000 以使其为毫秒 - UTC 时间戳传统上以秒为单位。
        猜你喜欢
        • 2015-09-23
        • 1970-01-01
        • 2021-04-03
        • 2019-09-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-07
        • 1970-01-01
        相关资源
        最近更新 更多