I keep getting the error "Uncaught TypeError: date3.getFullYear is not a function"
I have a date variable in both SQL and JS format (passed from another function as arguments):
min1 - in SQL format. [Like - '2017-08-13 0:0:00']
mindate - in JS format. [Like - 'Sun Aug 13 2017 00:00:00 GMT+0530 (India Standard Time)' ]
I am using these variables to create a new date variable called 'date3'. I have tried the following:
var date2 = min1.split(' ')[0]; // should give me '2017-08-13'
date3 = new Date(date2);
AND,
var date3 = new Date();
date3 = mindate;
AND,
var date3 = mindate;
AND,
var Y = parseInt(min1.split(' ')[0].split('-')[0]); // 2017
var M = parseInt(min1.split(' ')[0].split('-')[1]) - 1; // 7
var D = parseInt(min1.split(' ')[0].split('-')[2]); // 13
var date3 = new Date(Y,M,D);
And when I console.log the date3, it gives the correct date. But when I do a getFullYear() on date3, it gives the error: "Uncaught TypeError: getFullYear is not a function"
I have tried everything and nothing seems to work. (I even read some other similar questions, and some answers were saying its because 'date' is a 'moment' object, but I don't understand what it means and how it can help resolve this).
UPDATE:
Here's how I am using the getFullYear function. I have tried two things:
datestring = date3.getFullYear() + '-' + (date3.getMonth()+1) + '-' + (date3.getDate());
AND
var y = date3.getFullYear(),
m = date3.getMonth() + 1, // january is month 0 in javascript
d = date3.getDate();
Am using this inside a 'for' loop, and the date3 was created just above the loop as follows:
var date3 = new Date(Y,M,D);
console.log(date3);
for (k=1; k<=x; k++) {
var y = date3.getFullYear(),
m = date3.getMonth() + 1, // january is month 0 in javascript
d = date3.getDate();
var pad = function(val) { var str = val.toString(); return (str.length < 2) ? "0" + str : str};
datestring = [y, pad(m), pad(d)].join("-");
console.log("DATESTRING", datestring);
divCode += '<div class="swiper-slide" id="_' + datestring + '" data-hash="' + datestring + '"><b>' +datestring+ '</b><br><br>' +tableCode+ '</div>';
// Incrementing date by 1
date3 = date3.setDate(date3.getDate() + 1);
}
I keep getting the error "Uncaught TypeError: date3.getFullYear is not a function"
I have a date variable in both SQL and JS format (passed from another function as arguments):
min1 - in SQL format. [Like - '2017-08-13 0:0:00']
mindate - in JS format. [Like - 'Sun Aug 13 2017 00:00:00 GMT+0530 (India Standard Time)' ]
I am using these variables to create a new date variable called 'date3'. I have tried the following:
var date2 = min1.split(' ')[0]; // should give me '2017-08-13'
date3 = new Date(date2);
AND,
var date3 = new Date();
date3 = mindate;
AND,
var date3 = mindate;
AND,
var Y = parseInt(min1.split(' ')[0].split('-')[0]); // 2017
var M = parseInt(min1.split(' ')[0].split('-')[1]) - 1; // 7
var D = parseInt(min1.split(' ')[0].split('-')[2]); // 13
var date3 = new Date(Y,M,D);
And when I console.log the date3, it gives the correct date. But when I do a getFullYear() on date3, it gives the error: "Uncaught TypeError: getFullYear is not a function"
I have tried everything and nothing seems to work. (I even read some other similar questions, and some answers were saying its because 'date' is a 'moment' object, but I don't understand what it means and how it can help resolve this).
UPDATE:
Here's how I am using the getFullYear function. I have tried two things:
datestring = date3.getFullYear() + '-' + (date3.getMonth()+1) + '-' + (date3.getDate());
AND
var y = date3.getFullYear(),
m = date3.getMonth() + 1, // january is month 0 in javascript
d = date3.getDate();
Am using this inside a 'for' loop, and the date3 was created just above the loop as follows:
var date3 = new Date(Y,M,D);
console.log(date3);
for (k=1; k<=x; k++) {
var y = date3.getFullYear(),
m = date3.getMonth() + 1, // january is month 0 in javascript
d = date3.getDate();
var pad = function(val) { var str = val.toString(); return (str.length < 2) ? "0" + str : str};
datestring = [y, pad(m), pad(d)].join("-");
console.log("DATESTRING", datestring);
divCode += '<div class="swiper-slide" id="_' + datestring + '" data-hash="' + datestring + '"><b>' +datestring+ '</b><br><br>' +tableCode+ '</div>';
// Incrementing date by 1
date3 = date3.setDate(date3.getDate() + 1);
}
Share
Improve this question
edited Aug 13, 2017 at 23:35
RobG
148k32 gold badges179 silver badges214 bronze badges
asked Aug 13, 2017 at 20:08
coderDcoderD
2651 gold badge3 silver badges13 bronze badges
8
-
1
Update the code to show the exact usage of
date3.getFullYear()
– Dekel Commented Aug 13, 2017 at 20:12 - OK, so its working fine outside the for loop. – coderD Commented Aug 13, 2017 at 20:25
- I'm afraid I don't see any for loop in your code... – Dekel Commented Aug 13, 2017 at 20:26
- I am sorry, I updated it.. – coderD Commented Aug 13, 2017 at 20:30
- 1 Please see How to create a minimal, plete and verifiable example. It seems this is actually a duplicate of Add +1 to current date. Note also that "2017-08-13" will be parsed as UTC (mostly) so will appear to be the wrong date sometimes if the host offset is not +0000. – RobG Commented Aug 13, 2017 at 23:35
1 Answer
Reset to default 4The setDate
method works inplace on the object, and the return value of the function is the timestamp of the new date.
When you did:
date3 = date3.setDate(date3.getDate() + 1);
in your function, the date3
is now an integer (and not a Date() object), so basically what your code does now is 1502742691133.getFullYear()
, and this is wrong.
You don't need to set the value of setDate()
to date3
. Just do:
date3.setDate(date3.getDate() + 1);
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744840564a4596532.html
评论列表(0条)