大家好,欢迎来到IT知识分享网。
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;
using Microsoft.VisualBasic;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
long var_years = DateAndTime.DateDiff(DateInterval.Year, dateTimePicker1.Value, dateTimePicker2.Value);
textBox1.Text = var_years.ToString() + "年";
long var_months = DateAndTime.DateDiff(DateInterval.Month, dateTimePicker1.Value, dateTimePicker2.Value);
textBox2.Text = var_months.ToString() + "个月";
long var_days = DateAndTime.DateDiff(DateInterval.Day, dateTimePicker1.Value, dateTimePicker2.Value);
textBox3.Text = var_days.ToString() + "天";
}
private void button2_Click(object sender, EventArgs e)
{
//增加时间段
DateTime add_minute = DateAndTime.DateAdd(DateInterval.Minute,1, dateTimePicker3.Value);
tex_AddM.Text = add_minute.ToString("F") ;
DateTime add_hour = DateAndTime.DateAdd(DateInterval.Hour, 1, dateTimePicker3.Value);
tex_AddH.Text = add_hour.ToString("F");
DateTime add_day = DateAndTime.DateAdd(DateInterval.Day, 1, dateTimePicker3.Value);
tex_AddD.Text = add_day.ToString("F");
//减少时间段
DateTime subtract_minute = DateAndTime.DateAdd(DateInterval.Minute, -1, dateTimePicker3.Value);
tex_SubtractM.Text = subtract_minute.ToString("F");
DateTime subtract_hour = DateAndTime.DateAdd(DateInterval.Hour, -1, dateTimePicker3.Value);
tex_SubtractH.Text = subtract_hour.ToString("F");
DateTime subtract_day = DateAndTime.DateAdd(DateInterval.Day, -1, dateTimePicker3.Value);
tex_SubtractD.Text = subtract_day.ToString("F");
}
}
}
IT知识分享网
结语:
学会使用DateAndTime类中的DateDifff()方法和DateAdd()方法,并掌握输出指定格式的字符串。
喜欢的请关注和收藏!
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/6752.html