Compare two dates in JS

后端 未结 2 1859
灰色年华
灰色年华 2020-12-17 15:33

I want to compare the user\'s birthday against today\'s date and get the number of days in between. The birthday they enter will be in the form of 12/02/1987 in an

相关标签:
2条回答
  • 2020-12-17 15:56

    I wrote a lightweight date library called Moment.js to handle stuff like this.

    var birthday = moment('12/02/1987', 'MM-DD-YYYY');
    var inputDate = moment(element.value, 'MM-DD-YYYY');
    var diff = birthday.diff(inputDate, 'days'); 
    

    http://momentjs.com/docs/#/displaying/difference/

    0 讨论(0)
  • 2020-12-17 16:12

    You'll need to first parse element.value as a date:

    difference = today - new Date(element.value);
    

    http://jsfiddle.net/gilly3/3DKfy/

    0 讨论(0)
提交回复
热议问题