count days till new year in javascript

To count the days till the new year, you can use JavaScript's built-in Date object to get the current date and time, then calculate the difference between that and the first day of the next year.

Here's the code:

index.tsx
// Get the current date
const today = new Date();

// Get the current year and the next year
const currentYear = today.getFullYear();
const nextYear = currentYear + 1;

// Create a new Date object for the first day of next year
const newYear = new Date('January 1, ' + nextYear + ' 00:00:00');

// Calculate the difference between the current date and new year in milliseconds
const diff = newYear.getTime() - today.getTime();

// Convert the milliseconds to days and round down
const daysTillNewYear = Math.floor(diff / (1000 * 60 * 60 * 24));

// Output the result
console.log('There are ' + daysTillNewYear + ' days till the new year.');
641 chars
19 lines

This code gets the current date, determines the next year, creates a new Date object for the first day of next year, calculates the difference between that and the current date in milliseconds, converts that to days, and finally outputs the result as a message to the console.

gistlibby LogSnag