[Csharp] Enter the year and month, and output the number of days in the month

1 Title description: Enter the year and month, and output the number of days in the month

输入年和月份,输出本月有几天(窗体控制)

2 Detailed source code

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 Csharp1_2
{
    
    
    public partial class Form1 : Form
    {
    
    
        public Form1()
        {
    
    
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
    
    
            int year, month;
            year = Convert.ToInt32(textBox_year.Text);
            month = Convert.ToInt32(textBox_month.Text);
            if ( month == 1 )
            {
    
    
                res.Text = "31";
            }
            else if ( month == 3 )
            {
    
    
                res.Text = "31";
            }
            else if ( month == 5 )
            {
    
    
                res.Text = "31";
            }
            else if ( month == 7 )
            {
    
    
                res.Text = "31";
            }
            else if ( month == 8 )
            {
    
    
                res.Text = "31";
            }
            else if ( month == 10 )
            {
    
    
                res.Text = "31";
            } 
            else if ( month == 12 )
            {
    
    
                res.Text = "31";
            }
            else if ( month == 4 )
            {
    
    
                res.Text = "30";
            }
            else if ( month == 6 )
            {
    
    
                res.Text = "30";
            }
            else if ( month == 9 )
            {
    
    
                res.Text = "30";
            }
            else if ( month == 11 )
            {
    
    
                res.Text = "30";
            }
            else if ( month == 2 )
            {
    
    
                if ( year % 400 == 0 )
                {
    
    
                    res.Text = "29";
                }
                else if ( year % 100 == 0 )
                {
    
    
                    res.Text = "29";
                }
                else if ( year % 4 == 0 )
                {
    
    
                    res.Text = "29";
                }
                else
                {
    
    
                    res.Text = "28";
                }
            }
        }
    }
}

3 Achieve the effect

Insert picture description here

Guess you like

Origin blog.csdn.net/Gyangxixi/article/details/114318633