Are there any way to convert the GetFullYear() in real time seconds from the beginning of the current year? Instead of having a output of 2017, i have a dynamic seconds until the end of the year. As well as obtaining the maximum value of the GetFullYear() converted in seconds to make some calculations. Thanks in advance
-
well, a year isn't enough to know the seconds ... you need a year, month,day, hour, minutes seconds – Jaromanda X Feb 02 '17 at 05:32
-
Try to find difference between two dates in seconds. Refer this http://stackoverflow.com/questions/2024198/how-many-seconds-between-two-dates – RaR Feb 02 '17 at 05:47
2 Answers
var date1 = new Date("1/1/2017");
var date2 = new Date("2/2/2017");
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));//returns 32
var time_in_seconds = diffDays*60*60;// calculate the date in seconds.

- 1,644
- 13
- 18
-
1
-
when you do console.log(diffDays) , it shows no of days from 1 january to 2 February , and then multiplied it by 60*60 to change in seconds. – Shekhar Tyagi Feb 02 '17 at 12:48
-
Don't parse strings with the Date constructor, `new Date(2017,0,1)` is less to type and the outcome is certain. Not all days have 8.64e7 ms. ;-) – RobG Feb 02 '17 at 13:00
Are there any way to convert the GetFullYear() in real time seconds from the beginning of the current year?
To get the difference in milliseconds between two dates, just subtract them. To get ms from the start of the year, subtract now from a date for 1 Jan at 00:00:00, e.g.
function msSinceStartOfYear() {
var now = new Date()
return now - new Date(now.getFullYear(), 0);
}
console.log(msSinceStartOfYear());
As well as obtaining the maximum value of the GetFullYear() converted in seconds to make some calculations.
According to ECMA-262, the maximum time value is 9,007,199,254,740,992, which is approximately 285,616 years after the epoch of 1 Jan 1970, or the year 287,586. So that is the theoretical maximum value of getFullYear.
However:
new Date(9007199254740992)
returns null. The actual range is exactly 100,000,000 days, so to get the maximum value, multiply days by the number of milliseconds per day:
new Date(100000000 * 8.64e7).getUTCFullYear() // 275,760
converted to seconds:
new Date(Date.UTC(275760, 0)) / 1000 // 8,639,977,881,600 seconds
but I have no idea why you want to do that. I've used UTC to remove timezone differences. Using "local" timezone changes the values by the timezone offset.

- 142,382
- 31
- 172
- 209