c#上一周下一周代码

public partial class Form1 : Form

    {

        DateTime dtNow;

        public Form1()

        {

            InitializeComponent();





        }



        private void Form1_Load(object sender, EventArgs e)

        {

            

            label1.Text = DateTime.Now.ToString("yyyy-MM-dd");

            dtNow = DateTime.Now;

        }



        private void button1_Click(object sender, EventArgs e)

        {

            showDays(false);//上一周

        }



        private void button2_Click(object sender, EventArgs e)

        {

            showDays(true);//下一周

        }



        void showDays(bool isNext)

        {

            textBox1.Text = "";

            int k = isNext ? 1 : -1;

            DateTime d1 = dtNow.AddDays(k * 7);

            while (d1.DayOfWeek != DayOfWeek.Sunday)

            {

                d1 = d1.AddDays(k);

            }

            for (int i = 0; i < 7; i++)

            {

                textBox1.AppendText(d1.AddDays(i+1).ToString("yyyy-MM-dd") + Environment.NewLine);

            }

            dtNow = d1;//起始日期

        }

    }

 

你可能感兴趣的:(C#)