【问题标题】:Angular get current date to filter http.get requestAngular 获取当前日期以过滤 http.get 请求
【发布时间】:2017-12-28 13:37:43
【问题描述】:
  getStuff(currentUserId: any){
    const today = ?
    const currentUser = JSON.parse(localStorage.getItem('currentUser'));
    const token = currentUser.token;
    const headers = new Headers({'Content-Type': 'application/json','Authorization': 'Bearer ' + token})
    return this.http.get(this.url + '/odata/User('+currentUserId+')/Stuff$filter=Date ????',{headers: headers})}

在这个函数中,我从我的 Api 中得到了一些东西,我得到的所有东西都有一个日期。我想过滤 http.get 请求以获取今天或将来的日期。

我的问题是如何获得今天的日期以及如何过滤我想要的东西?

我的数据库中的日期格式是这样的,例如“2017-12-21 00:00:00.000”

【问题讨论】:

    标签: angular date httprequest


    【解决方案1】:

    你可以随意格式化日期,

    要么做:

    "$filter=" + date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() + " " + date.getHours() ...etc
    

    查看这里了解更多信息:https://www.w3schools.com/js/js_date_methods.asp

    或者使用javascript格式函数:

    date.toLocaleDateString();
    

    然后将其转换回服务器端的日期

    【讨论】:

    • 它告诉我这些功能不存在
    • 您需要使用 new Date() 创建一个日期;或将参数作为日期传递
    • 这不起作用...就像我现在有我的日期 "2017-12-28 00:00:00.000" 但过滤器不起作用 '...$filter=Date eq ' +date 那里恰好是该日期的 1 条记录,但它不起作用..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-17
    相关资源
    最近更新 更多