亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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#計算年齡內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論