给定数组形式的日期,获取年月日.年月日为什么获取不到
tDate['2023-05-05','2023-05-12']
for(var j = 0; j < tDate.length; j++){
console.log("rtDate:"+tDate[j])
console.log("tDate:"+new Date(tDate[j]))
var arri ="";
arri= tDate[j];
//获取调休日期的年月日
var tyear = new Date(arri).getFullYear;
var tmonth = new Date(arri).getMonth+1;
var tday = new Date(arri).getDate;
console.log("年月日:"+tyear+"-"+tmonth+"-"+tday)
if(my_year==tyear&&month1==tmonth&&tday==i){
t = true;
z = false;
}
控制台输出结果
rtDate:2023-05-12
tDate:Fri May 12 2023 08:00:00 GMT+0800 (中国标准时间)
年月日:function getFullYear() { [native code] }-function getMonth() { [native code] }1-function getDate() { [native code] }
在您提供的代码中,获取年月日的操作出了问题。具体地说,您引用 getFullYear、getMonth 和 getDate 属性时忘记了添加圆括号来调用它们。这可能导致错误的结果或类型错误。
请将以下代码行更改为:
var tyear = new Date(arri).getFullYear(); // 调用 getFullYear() 方法
var tmonth = new Date(arri).getMonth() + 1; // 调用 getMonth() 方法并加上 1
var tday = new Date(arri).getDate(); // 调用 getDate() 方法
这应该会为您提供正确的日期值,以便后续的比较和条件处理。
另外,请确保 tDate 数组包含有效的日期字符串,并且使用逗号(,)而不是单引号(')来分隔数组元素:
var tDate = ['2023-05-05', '2023-05-12']; // 使用逗号分隔数组元素
希望这个回答对您有所帮助!
该回答通过自己思路及引用到GPTᴼᴾᴱᴺᴬᴵ搜索,得到内容具体如下:
在上述代码中,获取年月日的代码有一些问题。getFullYear
、getMonth
和 getDate
都是 Date 对象的方法,需要使用 ()
调用。修改代码如下:
for (var j = 0; j < tDate.length; j++) {
console.log("rtDate:" + tDate[j]);
var arri = tDate[j];
//获取调休日期的年月日
var tyear = new Date(arri).getFullYear();
var tmonth = new Date(arri).getMonth() + 1;
var tday = new Date(arri).getDate();
console.log("年月日:" + tyear + "-" + tmonth + "-" + tday);
if (my_year == tyear && month1 == tmonth && tday == i) {
t = true;
z = false;
}
}
在上述代码中,我们在调用 getFullYear
、getMonth
和 getDate
方法时,使用了 ()
调用。另外,我们将获取年月日的代码移动到了循环内部,以便能够获取每个日期的年月日。
如果以上回答对您有所帮助,点击一下采纳该答案~谢谢