【问题标题】:How can I get actual date in this format?如何以这种格式获取实际日期?
【发布时间】:2017-05-13 04:38:01
【问题描述】:

我想将实际日期与我从服务器接收到的这样的格式进行比较: item.expires_date.slice "2016-11-28 22:10:57 等/格林威治标准时间"

在javascript中这怎么可能?专门用于 Etc/GMT 部分

如果我只是想比较一下 2016-11-28 我怎样才能做到这一点:

 var today = new Date().toISOString().slice(0, 10);

     if(item.expires_date.slice(0, 10) > today) {
 console.log("This item have expired");

     } else {

       console.log("this item has not expired" );
     }

       }

它不起作用,因为它带来的项目没有过期比较日期: 2016-11-28 - 2016-12-28 谢谢!

【问题讨论】:

    标签: javascript date


    【解决方案1】:

    由于 "Etc/GMT" 与 "GMT+00:00" 相同,您可以将其删除并从字符串中创建一个Date 对象:

    var s = "2016-11-28 22:10:57 Etc/GMT";
    var d = new Date(Date.parse(s.replace("Etc/", "")));
    console.log(d.toString());

    然后您可以将d 与当前日期进行比较。

    【讨论】:

    • 小心,剥离时区部分意味着它将被视为本地,因此代表每个时区中具有不同偏移量的不同时刻。那是必需的吗?还是应该始终是 GMT?
    • Etc/GMT 没有任何偏移量是 UTC 日期,应该这样解析,当区域设置日期与 UTC 不同(通常是这样)时,上述代码将失败。有关 Etc/GMT 的更多信息,请参阅 this answer
    猜你喜欢
    • 1970-01-01
    • 2019-07-13
    • 2013-08-16
    • 2016-05-29
    • 1970-01-01
    • 1970-01-01
    • 2020-05-29
    • 1970-01-01
    • 2012-10-22
    相关资源
    最近更新 更多