C 實現計算年齡的簡單方法彙總

2022-09-26 07:30:11 字數 1049 閱讀 7424

vs2010測試通過,主要思想是由出生日期和當前日期,兩個日期計算出年齡(歲、月、天)

using system;

using system.collections.generic;

using system.text;

namespace publicclass

}}再簡單一些:

public int calculateagecorrect(datetime birthdate, datetime now)

下面我們來看看常規方法:

方法1:

string m_str = "1984-04-04";

int m_y1 = datetime.parse(m_str).year;

int m_y2 = datetime.now.year;

int m_age = m_y2 - m_y1;

response.write(m_age);

方法2:

如果你將日期格式化為yyyymmdd,並且從當前日子減去生日,最後去除4個數字,就得到年齡了:)

我相信這樣的方法可以用任何語言實現:

20080814-19800703=280111

去除最後4位 = 28.

int now =int.parse(datetime.today.tostring("yyyymmdd"));

int dob =int.parse(datedob.tostring("yyyymmdd"));

string dif =(no程式設計客棧w - dob).tostring();

string age ="0";

if(dif.length>4)

age = dif.substring(0, dif.length-4);

方法3:

datetime now =datetime.today;

int age = now.year- bday.year;

if(bday > now.addyears(-age)) age--;

本文標題: c#實現計算年齡的簡單方法彙總

本文位址:

實現 SOA 安全的簡單方法

soa 安全實現起來可以是非常容易的 前提是你選擇的方式要正確。松耦合的 可發現的 可共用的安全工具 乙個簡單的工具 ws security 對 web services 進行了加強,為 soa 安全提供了你需要的一切 訪問控制 加密 信任 傳輸安全以及不可抵賴。訊息示例 ws security 將...

定位的系統實現簡單方法

1 匯入標頭檔案 import 2 遵循 cllocationmanagerdelegate 3 初始化變數 cllocationmanager locationmanager clgeocoder geocoder 4 實現 如果沒有授權則請求使用者授權 if cllocationmanager ...

通過簡單方法實現spring boot web專案

搭建效果為 直接在網頁輸入請求,在頁面中顯示一行文字 hello,spring boot 與一般的wen專案不同的地方 1 不需要配置web.xml 檔案,但需要註解 springbootapplication 等 2 一切和spring有關的jar包都不需要版本號,springcloud會給你選擇...