如何将新的Date()转换为JSON字符串? [英] How to convert new Date() into JSON string?

查看:224
本文介绍了如何将新的Date()转换为JSON字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在研究离子应用程序,我正在使用 Ionic Native Calendar Plugin 在我的项目中创建日历事件。我希望能够为每个事件动态更改日期和时间,因此我使用Firebase作为后端,并希望将日历参数集成到我的JSON文件中以实现此目的。

I'm currently working on an ionic app and I'm using the Ionic Native Calendar Plugin to create calendar events in my project. I want to be able to change the dates and times dynamically for each event so I'm using Firebase as the backend and would like to integrate the calendar parameters into my JSON file to achieve this.

按照标准方法,一切都运行良好,即有一个带功能的按钮:

Everything is working well following the standard method, which is, having a button with a function:

**home.html**

<button ion-button (click)="createEvent()">Add to Calendar</button>

然后声明函数:

**home.ts**

createEvent() {
    this.calendar.createEvent('myEventName', 'myEventLocation', 'myEventNote', new Date(2017, 9, 20, 13, 0, 0, 0), 
    new Date(2017, 9, 20, 14, 0, 0, 0)).then(() => {
      console.log('Event Created!');
    }).catch((err) => {
      console.log('Oops, something went wrong:', err);
    });
  }

如果我在项目中运行上述方法,则会成功创建事件。 然而 ,使用此方法将无法让我能够动态创建日历事件,这正是我想要实现的目标。所以不知何故,我需要将 home.ts 中的 createEvent()内的代码链接到我的 JSON 文件,对吗?所以这就是我到目前为止所尝试的,似乎没有任何工作,我不知道我哪里出错了 - 我怀疑我没有将新的Date()参数正确转换为可读的JSON串?以下是我在尝试通过JSON动态创建事件时所做的工作:

If I run this method above in my project the event gets created successfully. However, using this method will then not allow me to be able to create calendar events dynamically, which is what I'm trying to achieve. So somehow, I need to link up the code inside my createEvent() in home.ts file into my JSON file, right? So this is what I've tried so far and nothing seems to be working, and I don't know where I'm going wrong - I'm suspecting I'm not converting the new Date() parameters properly into a readable JSON string? Here is what I've done so far in trying to create my events dynamically via JSON:

home.html:

<button ion-button (click)="createEvent()">Add to Calendar</button>

home.ts:

import firebase from 'firebase';

export class HomePage {

  events = [];

constructor(..) {

    firebase.database().ref('events').on('value', snapshot => {
      this.events = snapshot.val();
    });

  }

createEvent() {
    this.calendar.createEvent(
      this.item.item[0].title,
      this.item.item[0].location,
      this.item.item[0].notes,
      this.item.item[0].startDate,
      this.item.item[0].endDate,
    ).
      then(() => {
      console.log('Event Created!');
    })
      .catch((err) => {
      console.log('Oops, something went wrong:', err);
    });
  }

JSON文件(在Firebase数据库中):

"events" : [ {
    "title" : "myEventName",
    "location" : "myEventLocation",
    "notes" : "myEventNote",
    "startDate" : "new Date(2017, 9, 20, 13, 0, 0, 0)",
    "endDate" : "new Date(2017, 9, 20, 14, 0, 0, 0)"
  }, {

所以,当我运行上面没有任何操作时,没有使用此方法创建日历事件。请协助。谢谢。

So, when I run the above nothing happens, no calendar event is getting created using this method. Please assist. Thanks.

推荐答案

经过一番窥探后,我使用以下方法让它工作......

After a bit of snooping around, I got it to work using the below method...

home.html:

<button ion-button (click)="createEvent()">Add to Calendar</button>

home.ts:

import firebase from 'firebase';

export class HomePage {

public events;
public item;

constructor(...) {

firebase.database().ref('events').on('value', snapshot => {
this.events = snapshot.val();
});

this.item.startDate = new Date(this.item.startDate);
this.item.endDate = new Date(this.item.endDate);

}

createEvent() {
    this.calendar.createEventInteractively(
      this.item.title,
      this.item.eventLocation,
      this.item.notes,
      this.item.startDate,
      this.item.endDate,
    ).
      then(() => {
      console.log('Event Created!');
    })
      .catch((err) => {
      console.log('Oops, something went wrong:', err);
    });
  }

JSON文件(在Firebase数据库中):

"events" : [ {
    "title" : "myEventName",
    "eventLocation" : "myEventLocation",
    "notes" : "myEventNote",
    "startDate" : "2010-05-10T10:20:00+02:00",
    "endDate" : "2010-05-10T18:30:00+02:00"
  }, {

这篇关于如何将新的Date()转换为JSON字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆