Converting a date from DD-MM-YYYY to DD/MM/YYYY in Javascript and finding the difference

前端 未结 2 983
一向
一向 2021-01-27 19:37

I am trying to convert a date (obtained from a datepicker field) in DD-MM-YYYY to DD/MM/YYYY and get the difference (in years) between the two..

I have tried this so far

2条回答
  •  难免孤独
    2021-01-27 20:23

    Try to make two Date- Objects like this

    var date1 = new Date(2010, 6, 17);
    var date2 = new Date(2013, 12, 18);
    var diff = new Date(date2.getTime() - date1.getTime());
    
    console.log(diff.getUTCFullYear() - 1970); // Gives difference as year
    // 3
    
    console.log(diff.getUTCMonth()); // Gives month count of difference
    // 6
    
    console.log(diff.getUTCDate() - 1); // Gives day count of difference
    // 4
    

提交回复
热议问题