Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ App.fn = App.prototype;
App.fn.load = function(){
var value;

if (value = localStorage.dob)
if (value = localStorage.dob) {
this.dob = new Date(parseInt(value));
this.dob.setTime(this.dob.getTime() + 1000 * 60 * this.dob.getTimezoneOffset());
}
};

App.fn.save = function(){
Expand All @@ -40,6 +42,7 @@ App.fn.submit = function(e){

this.dob = input.valueAsDate;
this.save();
this.dob.setTime(this.dob.getTime() + 1000 * 60 * this.dob.getTimezoneOffset());
this.renderAgeLoop();
};

Expand All @@ -51,17 +54,35 @@ App.fn.renderAgeLoop = function(){
this.interval = setInterval(this.renderAge.bind(this), 100);
};

App.fn.renderAge = function(){
var now = new Date
var duration = now - this.dob;
var years = duration / 31556900000;
App.fn.calculateAge = function(){
var now = new Date
var age = now.getFullYear() - this.dob.getFullYear();
var mDiff = now.getMonth() - this.dob.getMonth();

if (mDiff < 0 || (mDiff === 0 && now.getDate() < this.dob.getDate())) {
age--;
}

return age;
};

App.fn.calculateMS = function(){
var now = new Date
var janOne = new Date(now.getFullYear(), 0, 1);
var comingBirthday = new Date(this.dob.getTime());
var ms;

var majorMinor = years.toFixed(9).toString().split('.');
comingBirthday.setYear(now.getFullYear());
ms = (now.getTime() - janOne.getTime()) / (comingBirthday.getTime() - janOne.getTime());

return ms.toFixed(9).toString().split('.')[1];
};

App.fn.renderAge = function(){
requestAnimationFrame(function(){
this.html(this.view('age')({
year: majorMinor[0],
milliseconds: majorMinor[1]
year: this.calculateAge(),
milliseconds: this.calculateMS()
}));
}.bind(this));
};
Expand Down