2012-07-19 55 views
-6

我收到了一个关于部分的网站。有一些关于我的文字,包括我现在的年龄。懒惰,因为我不想在每个生日过后改变那个日期。自动更新时间

是否有一个简单的脚本根据我的出生日期显示我的年龄?我在网上没有找到类似的东西。

Thx提前。

+2

除了“我没有发现这样的网站上的任何东西。”是非常不可能的! 但是这里有一点帮助:http://www.codeproject.com/Questions/228987/Calculate-Age-from-Date-of-birth-using-Javscript-o – DerDee 2012-07-19 14:35:19

+0

没有什么像一个坏问题,只有不好的答案。 – 2012-07-19 15:03:18

回答

0

你可以用javascript来做到这一点。这应该是足够多的让你开始更多..

// The date you were born 
var birthDate = new Date(1990, 6, 19, 0, 0, 0, 0); 

// The current date 
var currentDate = new Date(); 

// The age in years 
var age = currentDate.getFullYear() - birthDate.getFullYear(); 

// Compare the months 
var month = currentDate.getMonth() - birthDate.getMonth(); 

// Compare the days 
var day = currentDate.getDate() - birthDate.getDate(); 

// If the date has already happened this year 
if (month < 0 || month == 0 && day < 0) 
{ 
    age--; 
} 

alert(age); 
一个事实,即它是一个坏的问题,
+1

半工作,因为每个人都在新年这样老化。 – DerDee 2012-07-19 14:36:44

+0

@DeDee全部给你。 <3 – 2012-07-19 14:43:53