【问题标题】:Angular 4 Date for today and tomorrow今天和明天的Angular 4日期
【发布时间】:2018-04-18 12:00:10
【问题描述】:

我想获得 2 个变量。 1 表示当前日期 (yyyy-MM-DD),1 表示明天日期 (yyyy-MM-DD)。

我需要将这 2 个变量放在我的 HTTP 请求中。 有人可以告诉我如何制作 2 个变量并在我的 HTTP 请求中使用它们。 当前后面的代码是这样的:

export class BookingService {

    private config: Object;
    public today = new Date();
    public tomorrow = new Date();
    public domainSettings: Object = {};

    constructor(
        private http: Http,
        private kioskservice: KioskService
    ) { }

    public getAllBookings() {
        return new Promise((resolve, reject) => {
            this.http
                .get(
                    `${this.kioskservice.getAPIUrl()}search/dashboard/${this.kioskservice.LocationGUID()}/?apikey=${this.kioskservice.getAPIKey()}&format=json&from=2018-04-17&until=2018-04-18&full=true`
                )
                .toPromise()
                .then(
                    res => {
                        this.config = res.json()
                        console.log(res.json());
                        resolve();
                    },
                    msg => {
                        throw new Error("Couldn't get all Bookings: " + msg);
                    }
                );
        });
    }
}

【问题讨论】:

标签: angular typescript


【解决方案1】:

您只需使用 JavaScript 的 Date 接口即可获得日期。

要在 HTTP 调用中使用它们,只需使用模板字符串添加它们。

public getAllBookings() {
    // Get the dates
    const today =  new Date();
    const tomorrow =  new Date(today.setDate(today.getDate() + 1));

    return new Promise((resolve, reject) => {
      this.http
        .get(
          `${this.kioskservice.getAPIUrl()}search/dashboard/${this.kioskservice.LocationGUID()}/?apikey=${this.kioskservice.getAPIKey()}&format=json&from=${this.dateFormat(today)}&until=${this.dateFormat(tomorrow)}&full=true`
        )
        .toPromise()
        .then(
          res => {
            this.config = res.json()
            console.log(res.json());
            resolve();
          },
          msg => {
            throw new Error("Couldn't get all Bookings: " + msg);
          }
        );
    });
  }

  // Helper function to format if you don't use moment.js or something alike
  private dateFormat(date: Date) {
    const day = date && date.getDate() || -1;
    const dayWithZero = day.toString().length > 1 ? day : '0' + day;
    const month = date && date.getMonth() + 1 || -1;
    const monthWithZero = month.toString().length > 1 ? month : '0' + month;
    const year = date && date.getFullYear() || -1;

    return `${year}-${monthWithZero}-${dayWithZero}`;
  }

如果您要大量处理日期、时间戳等,使用 moment.js 之类的东西会非常有用。

【讨论】:

  • 感谢它的工作!你也可以为昨天的日期做一个吗?
  • 嘿,没问题。但“昨天”的日期已经确定。见const tomorrow = new Date(today.setDate(today.getDate() + 1));。队友的欢呼声! ;)
  • 哦,还有 1 个问题。我刚刚听说我需要让它具有适应性,以便有些人可以设置它,时差为 3 小时,而不是 1 天。你知道怎么做这样的东西吗?
  • @MexLataster 然后你需要使用时间戳。无论如何,请在此处查看日期 API:developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…。调整代码以考虑时间。如果您无法管理,请创建一个新问题。但首先你必须展示一些书面代码,如果你不尝试人们会反对你的东西。 ;)
  • 感谢您的帮助,我去看看!
猜你喜欢
  • 1970-01-01
  • 2017-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-30
  • 1970-01-01
  • 2014-07-28
  • 1970-01-01
相关资源
最近更新 更多