欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

一、計(jì)算年齡的方法

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

二、 DateAndTime類

1.定義 

命名空間:

Microsoft.VisualBasic

程序集:

Microsoft.VisualBasic.Core.dll

DateAndTime 模塊包含在日期和時(shí)間操作中使用的過(guò)程和屬性。

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

2.常用方法

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

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

從 Date2 中減去 Date1 以給出一個(gè)長(zhǎng)值,指定兩個(gè) Date 值之間的時(shí)間間隔數(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方法計(jì)算年齡

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

// 使用DateDiff方法計(jì)算員工年齡
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 = "計(jì)算工齡",
                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 = "計(jì)算年齡:"
            };
            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ù)生日計(jì)算員工年齡";        
            groupBox1.ResumeLayout(false);
            groupBox1.PerformLayout();
        }
        /// <summary>
        /// 計(jì)算年齡
        /// </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()), "提示!");
        }
    }
}

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

相關(guān)文章

最新評(píng)論