Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
458 views
in Technique[技术] by (71.8m points)

javascript - AngularJS-Javascript在两个日期之间的天数(AngularJS - Javascript number of days between two dates)

I have two dates that I get in the following format - that are returned from a web service:(我有以下两种格式的日期-从Web服务返回的日期:)

var dateone = "2016-08-21T07:00:00.000Z"; var datetwo = "2016-08-28T07:00:00.000Z"; var datediff = datetwo - dateone; var numdays = Math.round(datediff); console.log("Number of Days is " + numdays); I get NaN .(我得到NaN 。) What am I missing here?(我在这里想念什么?)   ask by techwestcoastsfosea translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your dateone and datetwo variables are String s not Date s.(您的dateonedatetwo变量是String而不是Date 。)

Try this:(尝试这个:) var dateone = new Date("2016-08-21T07:00:00.000Z"); var datetwo = new Date("2016-08-28T07:00:00.000Z"); Also, substracting 2 Date s objects will give you the difference between them in milliseconds, if you want to determine the number of days, you can do something like this:(同样,减去2 Date对象将为您提供它们之间的差(以毫秒为单位),如果要确定天数,则可以执行以下操作:) var dayDif = (datetwo - dateone) / 1000 / 60 / 60 / 24;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...