Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JS日期。你离你的百年诞辰还有多远?_Javascript_Date_Datetime - Fatal编程技术网

Javascript JS日期。你离你的百年诞辰还有多远?

Javascript JS日期。你离你的百年诞辰还有多远?,javascript,date,datetime,Javascript,Date,Datetime,我的setFullYear()有问题。根据输入,我有一些日期,比如1980年1月1日,我应该知道过去的时间是多少:38年1个月10天14小时13分钟。但问题是我怎样才能把时间拖到一百周年纪念呢 我的解决方案: var now=新日期() var oldDate=新日期() now.setFullYear(now.getFullYear()-parseFloat(year1),now.getMonth()-parseFloat(month1),now.getDate()-parseFloat(d

我的
setFullYear()
有问题。根据输入,我有一些日期,比如1980年1月1日,我应该知道过去的时间是多少:38年1个月10天14小时13分钟。但问题是我怎样才能把时间拖到一百周年纪念呢

我的解决方案:

var now=新日期()
var oldDate=新日期()
now.setFullYear(now.getFullYear()-parseFloat(year1),now.getMonth()-parseFloat(month1),now.getDate()-parseFloat(daynumber1),now.getHours(),now.getMinutes();*//旧日*
oldDate.setFullYear(parseFloat(100)-now.getFullYear(),parseFloat(12)-now.getMonth(),parseFloat(30)-now.getDate(),parseFloat(24)-now.getHours(),parseFloat(60)-now.getMinutes();*//100年-(现在为旧日期)*
v2:

结果

var t=new Date('2018-07-01');
var t1=new Date('2018-05-31');
var diff = diffs(t , t1);
console.log(JSON.stringify(diff))

var t=new Date('2018-03-01');
var t1=new Date('2018-01-31');
var diff = diffs(t , t1);
console.log(JSON.stringify(diff))

var t=new Date('2016-03-01');
var t1=new Date('2016-01-31');
var diff = diffs(t , t1);
console.log(JSON.stringify(diff))  
{"years":0,"months":1,"days":1,"hours":0,"minutes":0,"seconds":0}
{"years":0,"months":1,"days":1,"hours":0,"minutes":0,"seconds":0}
{"years":0,"months":1,"days":1,"hours":0,"minutes":0,"seconds":0}

只需将数据转换为timeinmillis并减去这些数字

或者,如果你想减去或加上一年或一个月,只需减去与你的周期(如月份或一年)相等的时间(以毫秒为单位)

减去两个日期:

var now = new Date();

var oldData = new Date('date string in proper format');
//or set Year, month or day by yourself after creating the date object pointing to the current date.

var subtractedResult = now.getTime() - oldDate.getTime();
// now you have the diff in milliseconds 

//Milliseconds in a year = 31536000000

var diff in years = subtracted results / 31536000000;

类似地,如果您愿意,您可以使用单位秒,始终为
date
构造函数使用ISO 8601兼容的日期格式。当使用它时,获取[object object](与document.write一起使用)使用JSON.stringify()每个月都没有30天,任何一年都没有360天,甚至每天都没有24小时可以使用日光节约。这只是一个粗略的近似值<代码>新日期('1980-1-1')在某些浏览器中返回无效日期。@RobG这就是我编辑v2的原因。。“1980-1-1”是ISO格式。为什么
parseFloat(100)
?你期望结果是什么?
{"years":0,"months":1,"days":1,"hours":0,"minutes":0,"seconds":0}
{"years":0,"months":1,"days":1,"hours":0,"minutes":0,"seconds":0}
{"years":0,"months":1,"days":1,"hours":0,"minutes":0,"seconds":0}
var now = new Date();

var oldData = new Date('date string in proper format');
//or set Year, month or day by yourself after creating the date object pointing to the current date.

var subtractedResult = now.getTime() - oldDate.getTime();
// now you have the diff in milliseconds 

//Milliseconds in a year = 31536000000

var diff in years = subtracted results / 31536000000;