使用DateTime 從生日確定年齡
許多應用需要從生日的生日來計算一個人的年齡。 這是使用DateTime對象的簡潔有效的方法。>
方法:
這種方法提供了一種干淨的解決方案:
<code class="language-csharp">// Get today's date. DateTime today = DateTime.Today; // Calculate the age. int age = today.Year - birthdate.Year; // Adjust for cases where the birthday hasn't occurred yet this year. if (birthdate.Date > today.AddYears(-age)) age--;</code>
以上是如何使用DateTime從生日開始計算一個人的年齡?的詳細內容。更多資訊請關注PHP中文網其他相關文章!