【问题标题】:automatically update age自动更新年龄
【发布时间】:2012-07-18 18:26:54
【问题描述】:

我有一个带有 about 部分的网站。有一些关于我的文字,包括我现在的年龄。虽然我很懒,但我不想在每个生日后更改日期。

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

谢谢。

【问题讨论】:

  • 除了这是一个糟糕的问题,“我在网上没有找到类似的东西。”不太可能!但这里有一点帮助:codeproject.com/Questions/228987/…
  • 没有什么比不好的问题,只有不好的答案。

标签: html


【解决方案1】:

你可以用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 );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-19
    • 2020-10-20
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 2012-03-26
    • 1970-01-01
    相关资源
    最近更新 更多