我正在试图弄清楚为什么在将“无效日期”传递给var sDate = new Date(title)之后,我会一直得到它。我的目标是只得到这个月,这样我就可以在sqlite上查询它。

使用离子4,离子2-日历和角度离子-科多瓦。
我有以下的电离2-日历方法。
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,并尝试在节点上运行。
节点date.js
并给出以下输出。
sDate: 2020-04-30T16:00:00.000Z
我还试着运行代码片段,运行良好。但是在离子设备上,它不是吗?
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或设备进行调试。
发布于 2020-05-22 05:26:01
这就是我为解决我的问题所做的。谢谢你的建议,我应该通过一个有效的ISO 8601关于Safari。
我的解决方案就是把过去的几个月和一年分开,然后重新格式化它。
// 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)
现在起作用了。
https://stackoverflow.com/questions/61947661
复制相似问题