I have an object with a production_date
which I'm trying to extract year and month but I keep getting getMonth()
and getYear()
is not a function error...
function(d) {
var dt = d.production_date
var dtm = dt.getMonth();
var dty = dt.getYear();
return dtm + "/" + dty
}
function(d) {
var dt = new Date(d.production_date);
var dtm = dt.getMonth();
var dty = dt.getFullYear();
return dtm + "/" + dty
}
If it's passed over network, you'll only have a number (timestamp)
Make sure that is a valid date. If it is a string ,you can convert it into date using new Date("datestring"),make sure that datestring is in the following format "YYYY/MM/DD" ,if so you can do it as the following
var d = {
"production_date": "2016/11/23"
}
var val = getMonthYear(d);
console.log(val);
function getMonthYear(d) {
var dt = new Date(d.production_date);
var dtm = dt.getMonth();
var dty = dt.getYear();
return dtm + "/" + dty
}
Hope it helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With