【问题标题】:remove just 'UTC' text at the end from DateTime stamp从 DateTime 戳中删除末尾的“UTC”文本
【发布时间】:2013-04-29 19:55:09
【问题描述】:

我使用date.toGMTString() 获取DateTime 作为GMT 标准。所以我得到的是'Fri, 26 Apr 2013 17:08:35 UTC'这个。我只想在没有文本'UTC' 的情况下显示它。请告诉我

【问题讨论】:

  • date.toGMTString().substr(0, 25)

标签: javascript datetime utc


【解决方案1】:

使用slice:

date.toGMTString().slice(0, -4)

顺便说一句,您应该注意到toGMTString is deprecated and the same as toUTCString,并且该方法确实返回了一个依赖于实现人类可读的UTC日期字符串。你不能确定它是否以" UTC"(或" GMT")结尾,所以你宁愿使用

date.toUTCString().replace(/\s*(GMT|UTC)$/, "")

【讨论】:

  • 但是,如果它们都以您不想要的 4 个字符结尾,那是否会使 slice 变得依赖?或者有没有可能它不会以任何额外的东西结束?
  • 是的,有可能没有额外的(或者时区表示在字符串中的其他位置,或者......)
【解决方案2】:

toGMTString()deprecated...改用toUTCString()...

date.toUTCString().slice(0, -4)

【讨论】:

    【解决方案3】:
    var d= new Date();
    
    d=d.toLocaleString(); 
    
    //Converts a Date object to a string, using locale conventions
    //hence the UTC or GMT+.. is removed
    

    【讨论】:

      【解决方案4】:

      要获得最佳格式选项和跨浏览器一致性,请使用Moment.js

      var m = moment(date);
      var s = m.format('ddd, D MMM YYYY H:mm:ss');
      

      请参阅the docs 了解其他格式字符串,包括可本地化的字符串。

      【讨论】:

        猜你喜欢
        • 2012-06-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-27
        相关资源
        最近更新 更多