Moment.js not working as I expected for getting months between two dates
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
javascript momentjs
add a comment |
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
javascript momentjs
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 theformat
function. The link you got the code from doesn't do that..
– Keith
Nov 13 '18 at 12:12
add a comment |
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
javascript momentjs
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
javascript momentjs
javascript momentjs
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 theformat
function. The link you got the code from doesn't do that..
– Keith
Nov 13 '18 at 12:12
add a comment |
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 theformat
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
add a comment |
1 Answer
1
active
oldest
votes
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);
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 aconsole.log()
ofdata2[0]
anddata2[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
|
show 4 more comments
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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);
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 aconsole.log()
ofdata2[0]
anddata2[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
|
show 4 more comments
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);
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 aconsole.log()
ofdata2[0]
anddata2[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
|
show 4 more comments
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);
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);
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 aconsole.log()
ofdata2[0]
anddata2[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
|
show 4 more comments
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 aconsole.log()
ofdata2[0]
anddata2[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
|
show 4 more comments
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 theformat
function. The link you got the code from doesn't do that..– Keith
Nov 13 '18 at 12:12