C#使用DateAndTime.DateDiff方法计算年龄

目录

一、计算年龄的方法

二、 DateAndTime类

1.定义 

2.常用方法

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

三、使用DateAndTime.DateDiff方法计算年龄


一、计算年龄的方法

        使用DateDiff方法计算系统时间与员工生日之间相隔的年数来判断员工的年龄。同样地,也可以直接使用系统时间减去员工生日的时间,结果得到一个TimeSpan对象,通过TimeSpan对象的Days属性得到相隔的天数,使用相隔的天数除以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 个值之间的时间间隔数。

DateDiff(String, Object, Object, FirstDayOfWeek, FirstWeekOfYear)

从 中减去 Date1Date2 ,以提供一个长值,指定两 Date 个值之间的时间间隔数。

ToString()

返回表示当前对象的字符串。(继承自 Object)

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

        从 Date2 中减去 Date1 以给出一个长值,指定两个 Date 值之间的时间间隔数。

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);

参数
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静态方法可以方便地获取日期时间的间隔数。

// 使用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 = "根据生日计算员工年龄";        
            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()), "提示!");
        }
    }
}