I store user date of birth in timestamp. I calculate timestamp with javascript.
day = $("#birthday-day").val();
year = $("#birthday-year").val();
month = $("#birthday-month").val()-1;
date = new Date(Date.UTC(year, month, day));
birthdateTimestamp = Math.round(date.getTime() / 1000);
In Search page I have only age field. Depend on this field I need to display users that match this age. I calculate from age field value timestamp so I can use it in database as condition.
now = new Date();
age = $("#age").val();
date = new Date(Date.UTC(now.getFullYear()-age));
timestampYearsAgo = date.getTime()/1000;
My question:
Are these methods leap years safe or can I have any problems because of leap years? I am quite sure the first part is ok (correct me if not please), but for second part when I calculate timestamp from age, I am not sure how could be done to be leap years also considered. Does anybody have any idea?
Tnx
The date you create in the second sample corresponds to 1/1 the given year (since you only supply year, day and month default to 0). This is probably not what you want.
If you want people who is a certain age today, you should create a date which corresponds to today and then substract the years, e.g.:
If a birthday timestamp is between lowTimestamp and highTimestamp the age is correct.