【问题标题】:How to format jquery fullcalendar date as ISODate for MongoDB?如何将 jquery fullcalendar 日期格式化为 MongoDB 的 ISODate?
【发布时间】:2015-05-21 05:39:51
【问题描述】:

我想将我从 fullcalendar 的 dayClick 事件中获得的一天存储为 MongoDB 中的 ISODate。 dayClick:function(date,allDay,jsEvent,view){ ce.start = 日期; alert('点击:' + ce.start + '--' + date.format()); }) 结果是:

1425427200000--2015-03-04

但我需要在 mongo 中:

“开始”:ISODate(“2015-03-27T23:00:00.000Z”)

什么是正确的格式化规则。 ca.start = date.?????


编辑

我在 mongodb.org 上找到了这个,但我不能真正使用它。提供的答案总是返回 "开始" : "ISODate(\"2015-03-18T14:35:41.751Z\")", 但是我需要 "开始" : ISODate("2015-03-12T23:00:00.000Z"),

也许专家可以从以下方面获得更多信息:

mongo shell 使用 ISODate 助手包装 Date 类型的对象;但是,对象仍然是 Date 类型。

以下示例同时使用 new Date() 构造函数和 ISODate() 构造函数来返回 Date 对象。

var myDate = new Date(); var myDateInitUsingISODateWrapper = ISODate();

您也可以将 new 运算符与 ISODate() 构造函数一起使用。

要打印变量的值,请在 shell 中键入变量名,如下所示:

我的日期

结果是包装在 ISODate() 助手中的 myDate 的 Date 值:

ISODate("2012-12-19T06:01:17.171Z")

【问题讨论】:

  • 我对mongo不是很熟悉,你是通过nodejs访问的吗?
  • 如果我编辑的答案不起作用,您能否添加负责将此日期添加到数据库的代码?

标签: jquery mongodb fullcalendar


【解决方案1】:

Fullcalendar 使用momentjs 作为日期。

您可以随意format他们:

var formatstring = "YYYY-MM-DD";
date.format(formatstring);

对于 ISO 格式,请在此处查看我的答案:Convert system date to ISO format using momentjs

moment().utc().format("YYYY-MM-DDTHH:mm:ss.SSS[Z]");
//or
moment().utc().format('[ISODate("]YYYY-MM-DDTHH:mm:ss.SSS[Z")]');

编辑:

出于某种原因,我认为您需要一个字符串...我认为您实际上只需要另一个对象中的 JS 日期对象:

var obj = {
    start: moment().toDate()
};
//or
var start = moment().toDate();

再次编辑:

我查看了您的 previous question 并获得了更多背景信息。你可能想要:

select: function(start, end, allDay) {
            var ce = {};
            ce.start = start.toDate();
//...

【讨论】:

    【解决方案2】:

    你可以通过这样的方式使用.toISOString()

    var ISODate = new Date(date).toISOString();

    所以你的 dayClick 看起来像这样

    dayClick: function(date, allday, e, view){
       var ISODate = new Date(date).toISOString();
       console.log('ISODate("'+ISODate+'")');
    }
    

    这是demo

    【讨论】:

    • 谢谢,但这样做我最终会得到 ` "start" : "2015-03-28T00:00:00.000Z"` ` "end" : "2015-03-28T00:00:00.000 mongoDB 中的 Z"` 几乎没问题,但我使用 ISODate(.. 覆盖现有逻辑是否有进一步的格式化命令或者这个字符串操作?
    • 我不确定 mongodb 是如何获取日期的,但如果你想要它完全一样,那么是的,它只是一个字符串操作。我相应地更新了我的答案
    猜你喜欢
    • 2018-02-22
    • 1970-01-01
    • 2016-02-19
    • 2013-10-02
    • 2020-11-13
    • 1970-01-01
    • 2017-07-11
    • 2019-06-15
    • 1970-01-01
    相关资源
    最近更新 更多