[C#] DateTimePicker คำนวณผลต่างวันที่ในหน่วยวัน

ออกแบบหน้าตา


ใส่โค้ด

namespace datetimepickerNumberOfDay
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dateTimePickerStart.Format = DateTimePickerFormat.Short;
            dateTimePickerEnd.Format = DateTimePickerFormat.Long;
            DateTime today = DateTime.Now;
            dateTimePickerStart.Value = today;
            dateTimePickerEnd.Value = today.AddMonths(1);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int numOfDay = 0;
            numOfDay = dateTimePickerEnd.Value.Subtract(dateTimePickerStart.Value).Days;
            label3.Text = "ผลต่าง = "+numOfDay.ToString()+" วัน";
        }
    }
}


Previous
Next Post »