-3

Hi i have two dates in UI: To and From, in format dd.MM.YYYY HH:mm:ss I need to find the difference between the two dates in months. I tried the following to get month and year of the date but does not work:

var To = $("#startDate").val();
alert(To.getMonth() + To.getFullYear());

It gives NANANA as alert. P.S it works pretty fine for format YYYY-MM-dd HH:mm:ss but not for the above format

Wayne
  • 3
  • 1
  • 7
  • 1
    Why dont you use moment.js. You can just use the difference function to do this – DarkseidNG Jul 26 '17 at 14:13
  • @DarkseidNG Because including a whole library to do something once or twice is pointless, especially that this can be done with couple of lines in Vanilla Js. – Adrian Jul 26 '17 at 14:15
  • @Adriani6 It's literally the industry standard for using dates. Literally anyone worth their salt will tell you not to try to handle dates in vanilla javascript and just use momentjs. `A good programmer is a lazy programmer.` Why try to reinvent the wheel when it's already here? – jdmdevdotnet Jul 26 '17 at 14:33
  • @jdmdevdotnet Because I don't see a reason you need to include a (almost) 20kb library to only perform a simple function which literally takes couple of lines to product. Of course unless you use that functionality a lot, sometimes (for work especially) the page size is crucial for the product we are working on due to circumstances which users are in when accessing the website. This date can be parsed using vanilla js. – Adrian Jul 26 '17 at 14:48
  • @Adriani6 Wow 20KB??? How will I have room for anything else? LOL. Nah but 20KB is nothing. He can use moment for other things not just this. I just don't agree with you. The industry standard is momentjs. I've been through this too, asked questions using vanilla JS and the only answers I got were "use momentjs". And I learned why, after hours of trying to tinker with vanilla JS date. – jdmdevdotnet Jul 26 '17 at 14:53
  • @jdmdevdotnet You seem to miss my point. On the project we are working on in my company page size upon load is crucial and I'm not talking about the time it takes but the size of the page, so filling your project with libraries when it's pointless and unnecessary should be avoided (going back to my previous comment). I also fail to find anything to say that momentjs is an "industry standard". I agree, reinventing the wheel is pointless and should also be avoided, but something you need to look from both sides of the table. No point of including a whole library if not used extensively. – Adrian Jul 26 '17 at 14:58
  • 20KB is literally irrelevant though. Don't want to flood this with comments, so we can just agree to disagree. – jdmdevdotnet Jul 26 '17 at 15:00
  • You seem to be asking two questions: how to parse a particular format date string and how to get the difference between two dates. Both questions have been answered many, many times here and you have not made any attempt to answer the question yourself. – RobG Jul 27 '17 at 09:54

2 Answers2

0

Duplicate here: https://stackoverflow.com/a/34904248/4342197 But save your values like this.

var date = new Date();

var date1 = new Date("#startDate").val());
var date2 = d.getDate();
  • This does not even makes sense neither the tagged link does, as the accepted answer was limited to few scenarios – Wayne Jul 26 '17 at 14:21
  • @Wayne `This does not even make sense` Yes it actually does and it's pretty clear you have no clue what you're talking about. The answer is literally here in your face (what you did wrong). – jdmdevdotnet Jul 26 '17 at 14:31
  • @jdmdevdotnet it could be good to use moment.js but getDate() function is useless when the date format is different – Wayne Jul 26 '17 at 14:44
0

var To = moment($("#startDate").val(), "dd.MM.YYYY HH:mm:ss");
var From = moment($('#endDate').val(), "dd.MM.YYYY HH:mm:ss");
$('#MonthDiff').text(moment.duration(From.diff(To)).asMonths());
$('#YearDiff').text(moment.duration(From.diff(To)).asYears());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" value="01.07.2016 07:00:03" id="startDate" />
<input type="text" value="30.07.2017 13:43:46" id="endDate" />
<br>
Month Difference: <label id="MonthDiff"></label>
<br>
Year Difference: <label id="YearDiff"></label>
kots
  • 465
  • 2
  • 7
  • Your answer should not depend on a library that is neither tagged or mentioned in the OP. Also, your parse string is wrong, it should be "DD.MM.YYYY HH:mm:ss", note the capital DD. – RobG Jul 27 '17 at 10:00