Moment.js not working as I expected for getting months between two dates










0















I'm trying to get the month list between two dates. The start and end dates I took it from an array. I used moment.js to do this. Here is my code



 var Start = moment(new Date(data2[0])).format("YYYY-MM-DD")
var End = moment(new Date(data2[data2.length-1])).format("YYYY-MM-DD")

console.log(Start) //It prints 2018-08-08
console.log(End) //It prints 2019-01-04

var dateStart = moment(Start)
var dateEnd = moment(End)
var timeValues = ;


while (moment(dateEnd) > moment(dateStart) || moment(dateStart).format('MM') === moment(dateEnd).format('MM'))
timeValues.push(moment(dateStart).format('YYYY-MM'));
moment(dateStart).add(1,'month');


alert (timeValues)


I got this code from here



The error I'm getting is



TypeError: dateStart.format is not a function


Please correct me where I'm doing wrong



console
console.log










share|improve this question



















  • 1





    moment(data2[0]).format("DD/MM/YYYY"); You are not creating a moment date, you have now made it into a string, and yes, strings don't have format.

    – Keith
    Nov 13 '18 at 11:41











  • edited the question. please check now

    – Aroon
    Nov 13 '18 at 12:02











  • It's still the same,. dateStart is a string, because you made it into a string using the format function. The link you got the code from doesn't do that..

    – Keith
    Nov 13 '18 at 12:12















0















I'm trying to get the month list between two dates. The start and end dates I took it from an array. I used moment.js to do this. Here is my code



 var Start = moment(new Date(data2[0])).format("YYYY-MM-DD")
var End = moment(new Date(data2[data2.length-1])).format("YYYY-MM-DD")

console.log(Start) //It prints 2018-08-08
console.log(End) //It prints 2019-01-04

var dateStart = moment(Start)
var dateEnd = moment(End)
var timeValues = ;


while (moment(dateEnd) > moment(dateStart) || moment(dateStart).format('MM') === moment(dateEnd).format('MM'))
timeValues.push(moment(dateStart).format('YYYY-MM'));
moment(dateStart).add(1,'month');


alert (timeValues)


I got this code from here



The error I'm getting is



TypeError: dateStart.format is not a function


Please correct me where I'm doing wrong



console
console.log










share|improve this question



















  • 1





    moment(data2[0]).format("DD/MM/YYYY"); You are not creating a moment date, you have now made it into a string, and yes, strings don't have format.

    – Keith
    Nov 13 '18 at 11:41











  • edited the question. please check now

    – Aroon
    Nov 13 '18 at 12:02











  • It's still the same,. dateStart is a string, because you made it into a string using the format function. The link you got the code from doesn't do that..

    – Keith
    Nov 13 '18 at 12:12













0












0








0








I'm trying to get the month list between two dates. The start and end dates I took it from an array. I used moment.js to do this. Here is my code



 var Start = moment(new Date(data2[0])).format("YYYY-MM-DD")
var End = moment(new Date(data2[data2.length-1])).format("YYYY-MM-DD")

console.log(Start) //It prints 2018-08-08
console.log(End) //It prints 2019-01-04

var dateStart = moment(Start)
var dateEnd = moment(End)
var timeValues = ;


while (moment(dateEnd) > moment(dateStart) || moment(dateStart).format('MM') === moment(dateEnd).format('MM'))
timeValues.push(moment(dateStart).format('YYYY-MM'));
moment(dateStart).add(1,'month');


alert (timeValues)


I got this code from here



The error I'm getting is



TypeError: dateStart.format is not a function


Please correct me where I'm doing wrong



console
console.log










share|improve this question
















I'm trying to get the month list between two dates. The start and end dates I took it from an array. I used moment.js to do this. Here is my code



 var Start = moment(new Date(data2[0])).format("YYYY-MM-DD")
var End = moment(new Date(data2[data2.length-1])).format("YYYY-MM-DD")

console.log(Start) //It prints 2018-08-08
console.log(End) //It prints 2019-01-04

var dateStart = moment(Start)
var dateEnd = moment(End)
var timeValues = ;


while (moment(dateEnd) > moment(dateStart) || moment(dateStart).format('MM') === moment(dateEnd).format('MM'))
timeValues.push(moment(dateStart).format('YYYY-MM'));
moment(dateStart).add(1,'month');


alert (timeValues)


I got this code from here



The error I'm getting is



TypeError: dateStart.format is not a function


Please correct me where I'm doing wrong



console
console.log







javascript momentjs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 12:47







Aroon

















asked Nov 13 '18 at 11:24









AroonAroon

413314




413314







  • 1





    moment(data2[0]).format("DD/MM/YYYY"); You are not creating a moment date, you have now made it into a string, and yes, strings don't have format.

    – Keith
    Nov 13 '18 at 11:41











  • edited the question. please check now

    – Aroon
    Nov 13 '18 at 12:02











  • It's still the same,. dateStart is a string, because you made it into a string using the format function. The link you got the code from doesn't do that..

    – Keith
    Nov 13 '18 at 12:12












  • 1





    moment(data2[0]).format("DD/MM/YYYY"); You are not creating a moment date, you have now made it into a string, and yes, strings don't have format.

    – Keith
    Nov 13 '18 at 11:41











  • edited the question. please check now

    – Aroon
    Nov 13 '18 at 12:02











  • It's still the same,. dateStart is a string, because you made it into a string using the format function. The link you got the code from doesn't do that..

    – Keith
    Nov 13 '18 at 12:12







1




1





moment(data2[0]).format("DD/MM/YYYY"); You are not creating a moment date, you have now made it into a string, and yes, strings don't have format.

– Keith
Nov 13 '18 at 11:41





moment(data2[0]).format("DD/MM/YYYY"); You are not creating a moment date, you have now made it into a string, and yes, strings don't have format.

– Keith
Nov 13 '18 at 11:41













edited the question. please check now

– Aroon
Nov 13 '18 at 12:02





edited the question. please check now

– Aroon
Nov 13 '18 at 12:02













It's still the same,. dateStart is a string, because you made it into a string using the format function. The link you got the code from doesn't do that..

– Keith
Nov 13 '18 at 12:12





It's still the same,. dateStart is a string, because you made it into a string using the format function. The link you got the code from doesn't do that..

– Keith
Nov 13 '18 at 12:12












1 Answer
1






active

oldest

votes


















0














When you use .format("DD/MM/YYYY"); you are returning a String.



A string does not have format function as the error message states.



 v -- error here
var dateStart = moment(data2[0]).format("DD/MM/YYYY");


The line above returns a String, you want to use format function in a moment Date object.



Try something like this (assumnig that data2[data2.length-1] and data2[0] are valid Dates):



 var dateStart = moment(new Date(data2[0]));
var dateEnd = moment(new Date(data2[data2.length-1]));


I have made a live example using code pen check it out.




Updated question



Try using this code



var dateStart = moment("2018-08-08").format("DD/MM/YYYY"); //#08/08/2018
var dateEnd = moment("2019-04-01").format("DD/MM/YYYY"); //#01/04/2019

var timeValues = ;
dateEndObj = moment(dateEnd);
dateStartObj = moment(dateStart);

while (dateEndObj > dateStartObj || dateStartObj.format('MM') === dateEndObj.format('MM'))
timeValues.push(dateStartObj.format('YYYY-MM'));
dateStartObj.add(1,'month');


alert (timeValues);





share|improve this answer

























  • edited the question bt still have the same error

    – Aroon
    Nov 13 '18 at 12:01











  • Why are you using format in the first two lines, that is the error, because format is returning a string, can you show a console.log() of data2[0] and data2[data2.length-1]?

    – JuanDM
    Nov 13 '18 at 12:11












  • please find the console

    – Aroon
    Nov 13 '18 at 12:19











  • I didn't understand your last comment, check the provided link

    – JuanDM
    Nov 13 '18 at 12:20












  • from your live example I understand it is working but I need to change the date format since the array returns the different format

    – Aroon
    Nov 13 '18 at 12:22










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53279978%2fmoment-js-not-working-as-i-expected-for-getting-months-between-two-dates%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














When you use .format("DD/MM/YYYY"); you are returning a String.



A string does not have format function as the error message states.



 v -- error here
var dateStart = moment(data2[0]).format("DD/MM/YYYY");


The line above returns a String, you want to use format function in a moment Date object.



Try something like this (assumnig that data2[data2.length-1] and data2[0] are valid Dates):



 var dateStart = moment(new Date(data2[0]));
var dateEnd = moment(new Date(data2[data2.length-1]));


I have made a live example using code pen check it out.




Updated question



Try using this code



var dateStart = moment("2018-08-08").format("DD/MM/YYYY"); //#08/08/2018
var dateEnd = moment("2019-04-01").format("DD/MM/YYYY"); //#01/04/2019

var timeValues = ;
dateEndObj = moment(dateEnd);
dateStartObj = moment(dateStart);

while (dateEndObj > dateStartObj || dateStartObj.format('MM') === dateEndObj.format('MM'))
timeValues.push(dateStartObj.format('YYYY-MM'));
dateStartObj.add(1,'month');


alert (timeValues);





share|improve this answer

























  • edited the question bt still have the same error

    – Aroon
    Nov 13 '18 at 12:01











  • Why are you using format in the first two lines, that is the error, because format is returning a string, can you show a console.log() of data2[0] and data2[data2.length-1]?

    – JuanDM
    Nov 13 '18 at 12:11












  • please find the console

    – Aroon
    Nov 13 '18 at 12:19











  • I didn't understand your last comment, check the provided link

    – JuanDM
    Nov 13 '18 at 12:20












  • from your live example I understand it is working but I need to change the date format since the array returns the different format

    – Aroon
    Nov 13 '18 at 12:22















0














When you use .format("DD/MM/YYYY"); you are returning a String.



A string does not have format function as the error message states.



 v -- error here
var dateStart = moment(data2[0]).format("DD/MM/YYYY");


The line above returns a String, you want to use format function in a moment Date object.



Try something like this (assumnig that data2[data2.length-1] and data2[0] are valid Dates):



 var dateStart = moment(new Date(data2[0]));
var dateEnd = moment(new Date(data2[data2.length-1]));


I have made a live example using code pen check it out.




Updated question



Try using this code



var dateStart = moment("2018-08-08").format("DD/MM/YYYY"); //#08/08/2018
var dateEnd = moment("2019-04-01").format("DD/MM/YYYY"); //#01/04/2019

var timeValues = ;
dateEndObj = moment(dateEnd);
dateStartObj = moment(dateStart);

while (dateEndObj > dateStartObj || dateStartObj.format('MM') === dateEndObj.format('MM'))
timeValues.push(dateStartObj.format('YYYY-MM'));
dateStartObj.add(1,'month');


alert (timeValues);





share|improve this answer

























  • edited the question bt still have the same error

    – Aroon
    Nov 13 '18 at 12:01











  • Why are you using format in the first two lines, that is the error, because format is returning a string, can you show a console.log() of data2[0] and data2[data2.length-1]?

    – JuanDM
    Nov 13 '18 at 12:11












  • please find the console

    – Aroon
    Nov 13 '18 at 12:19











  • I didn't understand your last comment, check the provided link

    – JuanDM
    Nov 13 '18 at 12:20












  • from your live example I understand it is working but I need to change the date format since the array returns the different format

    – Aroon
    Nov 13 '18 at 12:22













0












0








0







When you use .format("DD/MM/YYYY"); you are returning a String.



A string does not have format function as the error message states.



 v -- error here
var dateStart = moment(data2[0]).format("DD/MM/YYYY");


The line above returns a String, you want to use format function in a moment Date object.



Try something like this (assumnig that data2[data2.length-1] and data2[0] are valid Dates):



 var dateStart = moment(new Date(data2[0]));
var dateEnd = moment(new Date(data2[data2.length-1]));


I have made a live example using code pen check it out.




Updated question



Try using this code



var dateStart = moment("2018-08-08").format("DD/MM/YYYY"); //#08/08/2018
var dateEnd = moment("2019-04-01").format("DD/MM/YYYY"); //#01/04/2019

var timeValues = ;
dateEndObj = moment(dateEnd);
dateStartObj = moment(dateStart);

while (dateEndObj > dateStartObj || dateStartObj.format('MM') === dateEndObj.format('MM'))
timeValues.push(dateStartObj.format('YYYY-MM'));
dateStartObj.add(1,'month');


alert (timeValues);





share|improve this answer















When you use .format("DD/MM/YYYY"); you are returning a String.



A string does not have format function as the error message states.



 v -- error here
var dateStart = moment(data2[0]).format("DD/MM/YYYY");


The line above returns a String, you want to use format function in a moment Date object.



Try something like this (assumnig that data2[data2.length-1] and data2[0] are valid Dates):



 var dateStart = moment(new Date(data2[0]));
var dateEnd = moment(new Date(data2[data2.length-1]));


I have made a live example using code pen check it out.




Updated question



Try using this code



var dateStart = moment("2018-08-08").format("DD/MM/YYYY"); //#08/08/2018
var dateEnd = moment("2019-04-01").format("DD/MM/YYYY"); //#01/04/2019

var timeValues = ;
dateEndObj = moment(dateEnd);
dateStartObj = moment(dateStart);

while (dateEndObj > dateStartObj || dateStartObj.format('MM') === dateEndObj.format('MM'))
timeValues.push(dateStartObj.format('YYYY-MM'));
dateStartObj.add(1,'month');


alert (timeValues);






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 12:50

























answered Nov 13 '18 at 11:42









JuanDMJuanDM

757519




757519












  • edited the question bt still have the same error

    – Aroon
    Nov 13 '18 at 12:01











  • Why are you using format in the first two lines, that is the error, because format is returning a string, can you show a console.log() of data2[0] and data2[data2.length-1]?

    – JuanDM
    Nov 13 '18 at 12:11












  • please find the console

    – Aroon
    Nov 13 '18 at 12:19











  • I didn't understand your last comment, check the provided link

    – JuanDM
    Nov 13 '18 at 12:20












  • from your live example I understand it is working but I need to change the date format since the array returns the different format

    – Aroon
    Nov 13 '18 at 12:22

















  • edited the question bt still have the same error

    – Aroon
    Nov 13 '18 at 12:01











  • Why are you using format in the first two lines, that is the error, because format is returning a string, can you show a console.log() of data2[0] and data2[data2.length-1]?

    – JuanDM
    Nov 13 '18 at 12:11












  • please find the console

    – Aroon
    Nov 13 '18 at 12:19











  • I didn't understand your last comment, check the provided link

    – JuanDM
    Nov 13 '18 at 12:20












  • from your live example I understand it is working but I need to change the date format since the array returns the different format

    – Aroon
    Nov 13 '18 at 12:22
















edited the question bt still have the same error

– Aroon
Nov 13 '18 at 12:01





edited the question bt still have the same error

– Aroon
Nov 13 '18 at 12:01













Why are you using format in the first two lines, that is the error, because format is returning a string, can you show a console.log() of data2[0] and data2[data2.length-1]?

– JuanDM
Nov 13 '18 at 12:11






Why are you using format in the first two lines, that is the error, because format is returning a string, can you show a console.log() of data2[0] and data2[data2.length-1]?

– JuanDM
Nov 13 '18 at 12:11














please find the console

– Aroon
Nov 13 '18 at 12:19





please find the console

– Aroon
Nov 13 '18 at 12:19













I didn't understand your last comment, check the provided link

– JuanDM
Nov 13 '18 at 12:20






I didn't understand your last comment, check the provided link

– JuanDM
Nov 13 '18 at 12:20














from your live example I understand it is working but I need to change the date format since the array returns the different format

– Aroon
Nov 13 '18 at 12:22





from your live example I understand it is working but I need to change the date format since the array returns the different format

– Aroon
Nov 13 '18 at 12:22



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53279978%2fmoment-js-not-working-as-i-expected-for-getting-months-between-two-dates%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo