【问题标题】:Why does Ionic-2 Calendar gives "Invalid Date" after passing to js Date() and when calling onViewTitleChanged?为什么 Ionic-2 日历在传递给 js Date() 和调用 onViewTitleChanged 后会给出“无效日期”?
【发布时间】:2020-09-08 20:37:59
【问题描述】:

我想弄清楚为什么在将“无效日期”传递给var sDate = new Date(title) 后,我总是收到“无效日期”。我的目标是只获取当前月份,所以我可以在 sqlite 上查询它。

在 ionic-cordova 上使用 ionic 4、ionic2-calendar 和 angular。

我有以下 ionic2-calendar 方法。

onViewTitleChanged(title)
{
    console.log("onViewTitleChanged:")
    console.log(title)

    var sDate = new Date(title)
    console.log("check Date:")
    console.log(sDate)

    console.log("typeof:")
    console.log(typeof title);
}

输出以下内容。

onViewTitleChanged: 2020 年 5 月

检查日期: 无效日期

类型: 字符串

我尝试运行单个文件 date.js 并尝试在节点上运行。

节点日期.js

并给出以下输出。

s日期: 2020-04-30T16:00:00.000Z

我也试过运行code-sn-p,效果很好。但是在离子设备上,它没有?

var title = "May 2020"

console.log("onViewTitleChanged:")
console.log(title)

var sDate = new Date(title)
console.log("check Date:")
console.log(sDate)

console.log("typeof:")
console.log(typeof title);

我不明白为什么它不能在模拟器 (iOS) 上运行,使用 Safari 或设备进行调试。

【问题讨论】:

标签: javascript node.js ionic-framework ionic2-calendar


【解决方案1】:

这是我为解决我的问题所做的。感谢您建议我应该在 Safari 上通过有效的 ISO 8601。

我的解决方案只是拆分传递的月份和年份并重新格式化。

// Passed Month, Year Only
var title = "May 2020"

// Split Ionic Month and Year
var sd = title.split(" ")

// Format to Proper Date
var pd = sd[0] + ' 1, ' + sd[1]

var sDate = new Date(pd)
console.log("Proper Date Parsed:")
console.log(sDate)

var month = '0' + (sDate.getMonth() + 1);
var year = sDate.getFullYear();

console.log("Month(slice):" + month.slice(-2))
console.log("Year(slice):" + year)

现在工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-08
    • 1970-01-01
    • 2018-09-18
    • 2019-05-12
    • 2021-11-22
    相关资源
    最近更新 更多