如何使用Javascript找到两个日期之间的差异 [英] How do i find the difference between two dates using Javascript
本文介绍了如何使用Javascript找到两个日期之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望延长日期去特定日期,所以我试图用今天的日期检测特定日期。但这不起作用的是我的代码如果日期是下个月27我怎样才能获得剩余的日子go
I want to get reaming days to go particular date so i am trying to detection of particular date with today date. but this not working here is my code If the date is next month 27 how can i get remaining days to go
var date2=new Date();
var date1=27/5/2012;
var diff = date1.getDate()-date2.getDate();
var date_reaming = diff.getDate();
document.write(date_reaming + 'days to go');
推荐答案
Here is the answer, i found this from here my js-fiddle is here
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth();/* Returns the month (from 0-11) */
var curr_month_plus= curr_month+1; /* because if the month is 4 it will show output 3 so we have to add +1 with month*/
var curr_year = d.getFullYear();
function dstrToUTC(ds) {
var dsarr = ds.split("/");
var mm = parseInt(dsarr[0],10);
var dd = parseInt(dsarr[1],10);
var yy = parseInt(dsarr[2],10);
return Date.UTC(yy,mm-1,dd,0,0,0); }
function datediff(ds1,ds2) {
var d1 = dstrToUTC(ds1);
var d2 = dstrToUTC(ds2);
var oneday = 86400000;
return (d2-d1) / oneday; }
var a =curr_month_plus+ '/' + curr_date + '/' + curr_year;
var b;
b = "5/26/2012";
document.write(+datediff(a,b)+" day(s)<br>");
这篇关于如何使用Javascript找到两个日期之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文