There is a script that shows two numbers, the number of days from a certain date, and the number of days until a certain future date.

Javascript:

 d0 = new Date ('Nov 26, 2012');
 d1 = new Date ();
 d2 = new Date ('Nov 26, 2016');
 d3 = new Date ();
 dt = (d1.getTime () - d0.getTime ()) / (1000 * 60 * 60 * 24);
 ds = (d3.getTime () - d2.getTime ()) / (1000 * 60 * 60 * 24);
 document.write (Math.round (dt) + '/' + Math.round (ds));

It is necessary to prescribe the condition that the future number becomes cool and bold when it reaches a certain limit. For example, now in the output we have the number -71 (-70), but as soon as the number is less than 20, then the assignment condition will work and the number in the output will be cool and bold. The simpler the better.

Please tell me how to implement.

  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky ♦

1 answer 1

Actually, all you have to do is add the necessary check and formatting. If you make the code more readable, it might look something like this:

var past = + new Date('Nov 26, 2012'), // автоматичСскоС ΠΏΡ€ΠΈΠ²Π΅Π΄Π΅Π½ΠΈΠ΅ ΠΊ таймстампу future = + new Date('Sep 26, 2016'), // Π°Π½Π°Π»ΠΎΠ³ (new Date('xxx')).getTime(); now = + new Date(), featuredTimeStyles = 'color: red; font-weight: bold', millisecondsInADay = 1000*60*60*24, daysPassed, daysLeft, daysPassedFormatted, daysLeftFormatted; daysPassed = Math.round((now - past) / millisecondsInADay); daysLeft = Math.round((now - future) / millisecondsInADay); daysPassedFormatted = daysPassed; daysLeftFormatted = daysLeft >= -20 ? '<span style="' + featuredTimeStyles + '">' + daysLeft + '</span>' : daysLeft; document.write(daysPassedFormatted + ' / ' + daysLeftFormatted); 

There is still a lot to be improved, but I think the meaning is clear.

  • Alex, thank you. The solution is more than satisfying. The script is needed to track the end date of payment for domain names. - Alex Kliagin September