C#使用DateAndTime.DateDiff實現(xiàn)計算年齡
一、計算年齡的方法
使用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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Unity Sockect實現(xiàn)畫面實時傳輸案例原理解析
Socket是比較常用的一種通信方式,本文通過案例給大家介紹Unity Sockect實現(xiàn)畫面實時傳輸功能,感興趣的朋友一起看看吧2021-08-08支持windows與linux的php計劃任務的實現(xiàn)方法
這篇文章主要介紹了支持windows與linux的php計劃任務的實現(xiàn)方法,較為詳細的講述了php計劃任務中涉及到的php程序?qū)崿F(xiàn)方法、Windows計劃任務實現(xiàn)方法等,需要的朋友可以參考下2014-11-11c# asp .net 動態(tài)創(chuàng)建sql數(shù)據(jù)庫表的方法
c# asp .net 動態(tài)創(chuàng)建sql數(shù)據(jù)庫表的方法,需要的朋友可以參考一下2013-04-04System.Data.OleDb.OleDbException: 未指定的錯誤的完美解決方法
本文給大家?guī)砣N有關System.Data.OleDb.OleDbException: 未指定的錯誤的完美解決方法,每種方法都很不錯,需要的朋友可以參考下2016-09-09