C#编程显示特定的日期格式,并且计算当前日期的前后日期源代码
使用C#编程显示特定的日期格式,并且计算当前日期的前后日期
界面:
源代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
inirq();
}
private string str_rq;
private void inirq()
{
comboBox1.Items.Add("2001/1/1");
comboBox1.Items.Add("2001年1月");
comboBox1.Items.Add("2001年1月1日");
comboBox1.Items.Add("2001年1月1日 00:00");
comboBox1.Items.Add("2001年1月1日 00:00:00");
comboBox1.Items.Add("自定义格式");
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if(comboBox1.SelectedIndex==0)
{
str_rq = "d";
}
else if (comboBox1.SelectedIndex == 1)
{
str_rq = "y";
}
else if (comboBox1.SelectedIndex == 2)
{
str_rq = "D";
}
else if (comboBox1.SelectedIndex == 3)
{
str_rq = "f";
}
else if (comboBox1.SelectedIndex == 4)
{
str_rq = "F";
}
else if (comboBox1.SelectedIndex == 5)
{
str_rq = "yyyy年MM月dd日 HH时mm分ss秒";
}
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = DateTime.Now.ToString(str_rq);
}
private void button2_Click(object sender, EventArgs e)
{
texToday.Text = DateTime.Now.ToString("D");
texYesterday.Text = DateTime.Now.AddDays(-1).ToString("D");
texTomorrow.Text = DateTime.Now.AddDays(1).ToString("D");
}
}
}
结语:
学会使用Items.Add方法添加下拉列表框和利用SelectedIndex选中索引进行判断,会使用DateTime.Now.ToString()方法的指定日期格式,并且掌握AddDays()方法计算当前时间的前后日期。
喜欢的请关注和收藏!
相关文章
- 可执行教学级关系型数据库原型(Spring Boot + Java)
- 130.C# Stack 堆栈_c# 堆栈分析
- Swift 算法实战之路:栈和队列_swift sort
- Python 栈:深度解析与应用_python3 栈
- 编写高效的AI编程提示词(Prompt)是确保AI生成高质量代码的关键
- 一次想不到的 Bootstrap 类加载器带来的 Native 内存泄露分析
- SpringBoot 自研运行时 SQL 调用树,3 分钟定位慢 SQL!
- 太可惜了,四面字节跳动,我的offer竟被一道“算法题”给拦截了
- BLACKPINK确定全团回归!又被Lisa、Jennie新发色美到哭
- 美国食品标签上的含义_美国食品标准的主要内容