1、首先创建一个窗体程序,布置窗体内容
namespace WorkingDays
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.dateTimePickerStartDate = new System.Windows.Forms.DateTimePicker();
this.dateTimePickerEndDate = new System.Windows.Forms.DateTimePicker();
this.buttonCalculate = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.labelWorkingDays = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// dateTimePickerStartDate
//
this.dateTimePickerStartDate.Format = System.Windows.Forms.DateTimePickerFormat.Short;
this.dateTimePickerStartDate.Location = new System.Drawing.Point(28, 41);
this.dateTimePickerStartDate.Name = "dateTimePickerStartDate";
this.dateTimePickerStartDate.Size = new System.Drawing.Size(88, 20);
this.dateTimePickerStartDate.TabIndex = 0;
//
// dateTimePickerEndDate
//
this.dateTimePickerEndDate.Format = System.Windows.Forms.DateTimePickerFormat.Short;
this.dateTimePickerEndDate.Location = new System.Drawing.Point(122, 41);
this.dateTimePickerEndDate.Name = "dateTimePickerEndDate";
this.dateTimePickerEndDate.Size = new System.Drawing.Size(88, 20);
this.dateTimePickerEndDate.TabIndex = 1;
//
// buttonCalculate
//
this.buttonCalculate.Location = new System.Drawing.Point(135, 80);
this.buttonCalculate.Name = "buttonCalculate";
this.buttonCalculate.Size = new System.Drawing.Size(75, 23);
this.buttonCalculate.TabIndex = 2;
this.buttonCalculate.Text = "计算";
this.buttonCalculate.UseVisualStyleBackColor = true;
this.buttonCalculate.Click += new System.EventHandler(this.buttonCalculate_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(25, 85);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(55, 13);
this.label1.TabIndex = 3;
this.label1.Text = "工作日:";
//
// labelWorkingDays
//
this.labelWorkingDays.AutoSize = true;
this.labelWorkingDays.Location = new System.Drawing.Point(81, 85);
this.labelWorkingDays.Name = "labelWorkingDays";
this.labelWorkingDays.Size = new System.Drawing.Size(0, 13);
this.labelWorkingDays.TabIndex = 4;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.labelWorkingDays);
this.Controls.Add(this.label1);
this.Controls.Add(this.buttonCalculate);
this.Controls.Add(this.dateTimePickerEndDate);
this.Controls.Add(this.dateTimePickerStartDate);
this.Name = "Form1";
this.Text = "小猪熊奉献";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.DateTimePicker dateTimePickerStartDate;
private System.Windows.Forms.DateTimePicker dateTimePickerEndDate;
private System.Windows.Forms.Button buttonCalculate;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label labelWorkingDays;
}
}
2、给按钮添加一个触发事件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WorkingDays
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonCalculate_Click(object sender, EventArgs e)
{
var startDate = this.dateTimePickerStartDate.Value;
var endDate = this.dateTimePickerEndDate.Value;
this.labelWorkingDays.Text = startDate.BusinessDaysUntil(endDate).ToString();
}
}
}
3、添加一个DateTimeExtention.cs文件
// -----------------------------------------------------------------------
// <copyright file="DateTimeExtention.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------
namespace WorkingDays
{
using System;
/// <summary>
/// TODO: Update summary.
/// </summary>
public static class DateTimeExtention
{
/// <summary>
/// Calculates number of business days, taking into account:
/// - weekends (Saturdays and Sundays)
/// - bank holidays in the middle of the week
/// </summary>
/// <param name="firstDay">First day in the time interval</param>
/// <param name="lastDay">Last day in the time interval</param>
/// <param name="bankHolidays">List of bank holidays excluding weekends</param>
/// <returns>Number of business days during the 'span'</returns>
public static int BusinessDaysUntil(this DateTime firstDay, DateTime lastDay, params DateTime[] bankHolidays)
{
firstDay = firstDay.Date;
lastDay = lastDay.Date;
if (firstDay > lastDay)
throw new ArgumentException("Incorrect last day " + lastDay);
TimeSpan span = lastDay - firstDay;
int businessDays = span.Days + 1;
int fullWeekCount = businessDays / 7;
// find out if there are weekends during the time exceedng the full weeks
if (businessDays > fullWeekCount * 7)
{
// we are here to find out if there is a 1-day or 2-days weekend
// in the time interval remaining after subtracting the complete weeks
int firstDayOfWeek = firstDay.DayOfWeek == DayOfWeek.Sunday ? 7 : (int)firstDay.DayOfWeek;
int lastDayOfWeek = lastDay.DayOfWeek == DayOfWeek.Sunday ? 7 : (int)lastDay.DayOfWeek;
if (lastDayOfWeek < firstDayOfWeek)
lastDayOfWeek += 7;
if (firstDayOfWeek <= 6)
{
if (lastDayOfWeek >= 7)// Both Saturday and Sunday are in the remaining time interval
businessDays -= 2;
else if (lastDayOfWeek >= 6)// Only Saturday is in the remaining time interval
businessDays -= 1;
}
else if (firstDayOfWeek <= 7 && lastDayOfWeek >= 7)// Only Sunday is in the remaining time interval
businessDays -= 1;
}
// subtract the weekends during the full weeks in the interval
businessDays -= fullWeekCount + fullWeekCount;
// subtract the number of bank holidays during the time interval
foreach (DateTime bankHoliday in bankHolidays)
{
DateTime bh = bankHoliday.Date;
if (firstDay <= bh && bh <= lastDay)
--businessDays;
}
return businessDays;
}
}
}
4、运行,大功告成。