最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#使用DateAndTime.DateDiff實現(xiàn)計算年齡

 更新時間:2024年01月24日 13:57:10   作者:wenchm  
這篇文章主要為大家詳細介紹了C#如何使用DateAndTime.DateDiff實現(xiàn)根據(jù)生日計算年齡,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下

一、計算年齡的方法

使用DateDiff方法計算系統(tǒng)時間與員工生日之間相隔的年數(shù)來判斷員工的年齡。同樣地,也可以直接使用系統(tǒng)時間減去員工生日的時間,結果得到一個TimeSpan對象,通過TimeSpan對象的Days屬性得到相隔的天數(shù),使用相隔的天數(shù)除以365即可得到員工的年齡。

二、 DateAndTime類

1.定義 

命名空間:

Microsoft.VisualBasic

程序集:

Microsoft.VisualBasic.Core.dll

DateAndTime 模塊包含在日期和時間操作中使用的過程和屬性。

[Microsoft.VisualBasic.CompilerServices.StandardModule]
public sealed class DateAndTime

2.常用方法

DateDiff(DateInterval, DateTime, DateTime, FirstDayOfWeek, FirstWeekOfYear)從 中減去 Date1Date2 ,以提供一個長值,指定兩 Date 個值之間的時間間隔數(shù)。
DateDiff(String, Object, Object, FirstDayOfWeek, FirstWeekOfYear)從 中減去 Date1Date2 ,以提供一個長值,指定兩 Date 個值之間的時間間隔數(shù)。
ToString()返回表示當前對象的字符串。(繼承自 Object)

3.DateDiff(DateInterval, DateTime, DateTime, FirstDayOfWeek, FirstWeekOfYear)

從 Date2 中減去 Date1 以給出一個長值,指定兩個 Date 值之間的時間間隔數(shù)。

public static long DateDiff (Microsoft.VisualBasic.DateInterval Interval, DateTime Date1, DateTime Date2, Microsoft.VisualBasic.FirstDayOfWeek DayOfWeek = Microsoft.VisualBasic.FirstDayOfWeek.Sunday, Microsoft.VisualBasic.FirstWeekOfYear WeekOfYear = Microsoft.VisualBasic.FirstWeekOfYear.Jan1);

參數(shù)

Interval    DateInterval
Required. A DateInterval enumeration value or a string expression representing the time interval you want to use as the unit of difference between Date1 and Date2.
 
Date1    DateTime
Required. The first date/time value you want to use in the calculation.
 
Date2    DateTime
Required. The second date/time value you want to use in the calculation.
 
DayOfWeek    FirstDayOfWeek
Optional. A value chosen from the FirstDayOfWeek enumeration that specifies the first day of the week. If not specified, Sunday is used.
 
WeekOfYear    FirstWeekOfYear
Optional. A value chosen from the FirstWeekOfYear enumeration that specifies the first week of the year. If not specified, Jan1 is used.
 
Returns    Int64
A long value specifying the number of time intervals between two Date values.
 
Exceptions    ArgumentException
Date1, Date2, or DayofWeek is out of range.
 
InvalidCastException
Date1 or Date2 is of an invalid type.

三、使用DateAndTime.DateDiff方法計算年齡

使用DateAndTime類的DateDiff靜態(tài)方法可以方便地獲取日期時間的間隔數(shù)。

// 使用DateDiff方法計算員工年齡
using Microsoft.VisualBasic;
 
namespace _055
{
    public partial class Form1 : Form
    {
        private GroupBox? groupBox1;
        private DateTimePicker? dateTimePicker1;
        private Label? label1;
        private Button? button1;
 
        public Form1()
        {
            InitializeComponent();
            Load += Form1_Load;
        }
        private void Form1_Load(object? sender, EventArgs e)
        {
            // 
            // dateTimePicker1
            // 
            dateTimePicker1 = new DateTimePicker
            {
                Location = new Point(104, 28),
                Name = "dateTimePicker1",
                Size = new Size(200, 23),
                TabIndex = 1
            };
            // 
            // label1
            //          
            label1 = new Label
            {
                AutoSize = true,
                Location = new Point(6, 34),
                Name = "label1",
                Size = new Size(68, 17),
                TabIndex = 0,
                Text = "選擇生日:"
            };
            // 
            // button1
            //           
            button1 = new Button
            {
                Location = new Point(134, 86),
                Name = "button1",
                Size = new Size(75, 23),
                TabIndex = 1,
                Text = "計算工齡",
                UseVisualStyleBackColor = true
            };
            button1.Click += Button1_Click;
            // 
            // groupBox1
            // 
            groupBox1 = new GroupBox
            {
                Location = new Point(12, 9),
                Name = "groupBox1",
                Size = new Size(310, 65),
                TabIndex = 0,
                TabStop = false,
                Text = "計算年齡:"
            };
            groupBox1.Controls.Add(dateTimePicker1);
            groupBox1.Controls.Add(label1);
            groupBox1.SuspendLayout();
 
            // 
            // Form1
            // 
            AutoScaleDimensions = new SizeF(7F, 17F);
            AutoScaleMode = AutoScaleMode.Font;
            ClientSize = new Size(334, 121);
            Controls.Add(button1);
            Controls.Add(groupBox1);
            Name = "Form1";
            StartPosition = FormStartPosition.CenterScreen;
            Text = "根據(jù)生日計算員工年齡";        
            groupBox1.ResumeLayout(false);
            groupBox1.PerformLayout();
        }
        /// <summary>
        /// 計算年齡
        /// </summary>
        private void Button1_Click(object? sender, EventArgs e)
        {
            long Age = DateAndTime.DateDiff(DateInterval.Year,
                 dateTimePicker1!.Value, DateTime.Now,
                 FirstDayOfWeek.Sunday, FirstWeekOfYear.Jan1);
            MessageBox.Show(string.Format("年齡為: {0}歲。",Age.ToString()), "提示!");
        }
    }
}

到此這篇關于C#使用DateAndTime.DateDiff實現(xiàn)計算年齡的文章就介紹到這了,更多相關C#計算年齡內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • C# 設計模式系列教程-狀態(tài)模式

    C# 設計模式系列教程-狀態(tài)模式

    狀態(tài)模式主要解決的是當控制一個對象狀態(tài)轉換的條件表達式過于復雜時的情況。把狀態(tài)的判斷邏輯轉移到表示不同的一系列類當中,可以把復雜的邏輯判斷簡單化。
    2016-06-06
  • C#用記事本編寫簡單WinForm窗體程序

    C#用記事本編寫簡單WinForm窗體程序

    這篇文章主要為大家詳細介紹了C#用記事本編寫簡單WinForm窗體程序,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-03-03
  • C# Npoi如何讀取單元格圖片并獲取所在單元格位置

    C# Npoi如何讀取單元格圖片并獲取所在單元格位置

    這篇文章主要介紹了C# Npoi如何讀取單元格圖片并獲取所在單元格位置,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • C# 委托的三種調用示例(同步調用 異步調用 異步回調)

    C# 委托的三種調用示例(同步調用 異步調用 異步回調)

    本文將主要通過同步調用、異步調用、異步回調三個示例來講解在用委托執(zhí)行同一個加法類的時候的的區(qū)別和利弊
    2013-12-12
  • C#實現(xiàn)將javascript文件編譯成dll文件的方法

    C#實現(xiàn)將javascript文件編譯成dll文件的方法

    這篇文章主要介紹了C#實現(xiàn)將javascript文件編譯成dll文件的方法,涉及C#編譯生成dll動態(tài)鏈接庫文件的實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-11-11
  • C#?wpf?Bitmap轉換成WriteableBitmap的方法

    C#?wpf?Bitmap轉換成WriteableBitmap的方法

    本文主要介紹了C#?wpf?Bitmap轉換成WriteableBitmap的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-08-08
  • c++指針使用形參改變實參的方法

    c++指針使用形參改變實參的方法

    下面小編就為大家?guī)硪黄猚++指針使用形參改變實參的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-03-03
  • 使用C#實現(xiàn)Excel與DataTable互轉的方案

    使用C#實現(xiàn)Excel與DataTable互轉的方案

    在企業(yè)級開發(fā)中,Excel文件與DataTable(數(shù)據(jù)表)的互轉是報表生成、數(shù)據(jù)遷移等場景的核心需求,傳統(tǒng)方案如OleDb或Office Interop存在內存泄漏、性能低下等問題,且依賴本地Office組件,所以本文給大家介紹了使用C#實現(xiàn)Excel與DataTable互轉的方案,需要的朋友可以參考下
    2025-08-08
  • C#延遲執(zhí)行方法函數(shù)實例講解

    C#延遲執(zhí)行方法函數(shù)實例講解

    這篇文章主要介紹了C#延遲執(zhí)行方法函數(shù)實例講解,這是比較常用的函數(shù),有需要的同學可以研究下
    2021-03-03
  • unity實現(xiàn)方向盤轉動效果

    unity實現(xiàn)方向盤轉動效果

    這篇文章主要為大家詳細介紹了unity實現(xiàn)方向盤轉動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09

最新評論

伊春市| 苏尼特右旗| 普兰县| 谷城县| 岑溪市| 泾阳县| 嘉义县| 襄城县| 灵台县| 武平县| 北碚区| 乌海市| 辰溪县| 灵山县| 正阳县| 东安县| 临澧县| 大悟县| 宝山区| 福清市| 铁力市| 新闻| 苏尼特右旗| 静海县| 历史| 南乐县| 潜山县| 宝应县| 镇沅| 安新县| 牟定县| 柞水县| 福建省| 额敏县| 疏附县| 天柱县| 东乡县| 宁南县| 喀喇沁旗| 噶尔县| 永嘉县|