Compare two dates in JS

后端 未结 2 1866
灰色年华
灰色年华 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/

提交回复
热议问题