Javascript Quit Smoking Meter
by Monica Shaw
http://www.spacekadet.org

This is a very rudementary counter that displays the time since you've quit smoking, the number of cigarettes not smoked, and money saved. It looks like this:

To use this, simply copy the code below into your website wherever you want the counter to appear. Adjust the quit date, time, etc. in the first 8 lines of the script to match your information. Also, feel free to change the text to your liking (if, for instance, you'd like it to read "$4 not spent killing myself"). There is a comment right above the portion of the script that does the actual printing.

Javascript code:

<script language="JavaScript"><!--

var quitday = 14; /* your quit day */
var quitmonth = 12; /* your quit month */
var quityear = 2003; /* your quit year */
var quithours = 0; /* your quit hours */
var quitminutes=0; /* your quit minutes */
var quitseconds=0; /* your quit seconds */
var cigsPerDay = 10; /* your cigarettes smoked per day */
var costPerPack = 4.00; /* your cost per pack of cigs */


var date = new Date();
var d = date.getDate();
var day = (d < 10) ? '0' + d : d;
var m = date.getMonth() + 1;
var month = (m < 10) ? '0' + m : m;
var yy = date.getYear();
var year = (yy < 1000) ? yy + 1900 : yy;
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();

function timeDifference(laterdate,earlierdate, cigs, cost) {
var difference = laterdate.getTime() - earlierdate.getTime();

var daysDifference = Math.floor(difference/1000/60/60/24);
difference -= daysDifference*1000*60*60*24
var hoursDifference = Math.floor(difference/1000/60/60);
difference -= hoursDifference*1000*60*60
var minutesDifference = Math.floor(difference/1000/60);
difference -= minutesDifference*1000*60
var secondsDifference = Math.floor(difference/1000);

var totalsmokes = 0;
var toalsaved = 0;

totalsmokes = Math.floor(cigs*daysDifference + (cigs*minutesDifference / (24*60) ));
totalsaved = (cost/20)*totalsmokes;

/* this is the line that actually prints the information. adjust it to your liking!! */
document.write('smoke free for <br>' + daysDifference + 'd ' + hoursDifference + 'h ' + minutesDifference + 'm ' + secondsDifference + 's <br>' + totalsmokes + ' cigs not smoked<br>$' + totalsaved + ' saved!<br>');
}

var laterdate = new Date(year,month,day,hours,minutes,seconds); // 1st January 2000
var earlierdate = new Date(quityear,quitmonth,quitday,quithours,quitminutes,quitseconds); // 13th March 1998

timeDifference(laterdate,earlierdate,cigsPerDay,costPerPack);

//--></script>