[removed] Calculate difference between two dates

前端 未结 4 1728
温柔的废话
温柔的废话 2021-01-15 03:23

I want to find difference between two Dates. For that I did subtract one Date object from another Date object. My code is as follows :

var d1 = new Date(); /         


        
4条回答
  •  礼貌的吻别
    2021-01-15 03:48

    If you need to be fairly accurate I suggest using days as your unit. Years have a variable number of days so do months, so saying "1 month" or "1 year" can mean different #s of days.

    var d1 = new Date(); //"now"
    var d2 = new Date(2012,3,17); // before one year
    var msPerDay = 1000*60*60*24;
    document.write( ((d1 - d2) / msPerDay).toFixed(0) + " days ago");
    

提交回复
热议问题