0

I got a problem with setFullYear(). On input, I have some date like 1/1/1980 and I should find how long have past time it's like: 38 years 1 month 10 days 14 hours 13 minutes. But the problem is in how can I get the time till centenary?

My solutions:

var now = new Date()

var oldDate = new Date()

now.setFullYear(now.getFullYear() - parseFloat(year1), now.getMonth() - parseFloat(month1), now.getDate() - parseFloat(daynumber1), now.getHours(), now.getMinutes());* // now-oldDate*

oldDate.setFullYear(parseFloat(100) - now.getFullYear(), parseFloat(12) - now.getMonth(), parseFloat(30) - now.getDate(), parseFloat(24) - now.getHours(), parseFloat(60) - now.getMinutes());* //100years - (now-oldDate)*

The problem is I can't minus or plus anything to now.getHours and now.getMinutes.

connexo
  • 53,704
  • 14
  • 91
  • 128
Daeron
  • 3
  • 1

2 Answers2

0

Just convert data to timeinmillis and subtract those numbers.

Or if you want to subtract or add a year or a month just subtract the timeinmillis equivalent to your period like month or a year

Subtracting two dates:

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;

similarly you can go as down to unit seconds if you like

Muhammad Danial Iqbal
  • 1,546
  • 1
  • 9
  • 10
-2
function convertMS(ms) {
  var d, h, m, s;
  s = Math.floor(ms / 1000);
  M = Math.floor(s / 60);
  h = Math.floor(M / 60);
  d = Math.floor(h / 24);
  m = Math.floor(d / 30);
  y = Math.floor(m / 12);
  s %= 60;
  M %= 60;
  h %= 24;
  d %= 30;
  m %= 12;
  return {years:y,months:m,days: d, hours: h, minutes: M, seconds: s };
};
var diff = (new Date() - new Date(1980,0,1));
console.log(convertMS(diff))

v2:

<script>

function daysInMonth (month, year) {
    return new Date(year, month+1, 0).getDate();
}

function diffs(a,b){
    y=(a.getFullYear())-(b.getFullYear())
    m=(a.getMonth())-(b.getMonth())
    d=(a.getDate())-(b.getDate())
    h=(a.getHours())-(b.getHours ())
    M=(a.getMinutes())-(b.getMinutes())
    s=(a.getSeconds())-(b.getSeconds())
    if (s<0){s+=60;M-=1;}
    if (M<0){M+=60;h-=1;}
    if (h<0){h+=24;d-=1;}
    var dim=daysInMonth(a.getMonth(),a.getFullYear())
    while (d<0){d+=dim;m-=1;}
    if (m<0){m+=12;y-=1;}
    return {years:y,months:m,days: d, hours: h, minutes: M,seconds:s};
}

var t=new Date();
var t1=new Date(1980,0,1);
var diff = diffs(t , t1);
document.write(JSON.stringify(diff))

</script>  

Samples

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))  

Results

{"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}
Smart Manoj
  • 5,230
  • 4
  • 34
  • 59
  • 1
    **Always** use ISO 8601 compatible date format for `Date` constructor. – connexo Mar 11 '18 at 12:40
  • When use it get [object Object] (Use with document.write) – Daeron Mar 11 '18 at 13:32
  • use JSON.stringify() – Smart Manoj Mar 11 '18 at 13:45
  • 1
    There are not 30 days in every month, nor 360 days in any year, not even 24 hours in every day where daylight saving is observed. This is a rough approximation only. `new Date('1980-1-1')` returns an invalid date in some browsers. – RobG Mar 11 '18 at 22:53
  • @RobG That's why I edited v2.. And '1980-1-1' it is the ISO Format.. – Smart Manoj Mar 12 '18 at 15:17
  • @SmartManoj—it's not the format required by [*ECMA-262*](http://ecma-international.org/ecma-262/8.0/#sec-date-time-string-format), which is based on ISO 8601 but not entirely consistent with it. `new Date('1980-1-1')` returns an invalid date in at least one current browser, which is entirely consistent with the language specification. It may be parsed as local (if considered not ISO 8601) or UTC if considered close enough. Again, both are consistent with ECMA-262 as it's entirely up to each implementation how non-compliant strings are parsed. – RobG Mar 13 '18 at 00:15
  • Oh, this answer is also produces incorrect results. For 2016-01-31 to 2016-03-01 it returns 1 month, -1 days and for 2018-01-31 to 2018-03-01 it returns 1 month, -2 days. There are other similar scenarios (e.g. 2018-05-31 to 2018-07-01) as it doesn't correctly deal with months of different lengths. – RobG Mar 13 '18 at 00:32
  • @RobG In which browser you tried – Smart Manoj Mar 13 '18 at 13:27
  • @SmartManoj—Safari, Firefox, Chrome, Opera. The host is irrelevant, the features you're using have been consistent across implementations for a very long time. The failure is in the logic, not the underlying host. – RobG Mar 14 '18 at 00:13