zoukankan      html  css  js  c++  java
  • 使用DateAdd方法向指定日期添加一段时间间隔,使用TimeSpan对象获取时间间隔

    一:使用DateAdd方法向指定日期添加一段时间间隔,截图

    二:代码

    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;
    using Microsoft.VisualBasic;
    
    namespace AddDate
    {
        public partial class Frm_Main : Form
        {
            public Frm_Main()
            {
                InitializeComponent();
            }
    
            private DateTime G_datetime;//定义时间字段
            private void Frm_Main_Load(object sender, EventArgs e)
            {
                G_datetime=DateTime.Now;//得到系统当前时间
                lab_time.Text = G_datetime.ToString(//显示时间信息
                    "时间:  yyyy年M月d日 H时m分s秒");
            }
            /*参数
                Interval
                类型:Microsoft.VisualBasic.DateInterval
                必需。 表示要加上的时间间隔的 DateInterval 枚举值或 String 表达式。 
                Number
                类型:System.Double
                必需。 Double . 表示希望添加的时间间隔数的浮点表达式。 Number 可以为正数(此时将获取今后的日期/时间值),也可以为负数(此时将获取过去的日期/时间值)。 在 Interval 指定小时、分钟或秒时,该参数可以包含小数部分。 对于其他类型的 Interval 值,将忽略 Number 的所有小数部分。 
                DateValue
                类型:System.DateTime
                必需。 Date . 表示要在其基础上加上此时间间隔的日期和时间的表达式。 DateValue 本身在调用程序中并未改变。 
                返回值
                类型:System.DateTime
                返回一个 Date 值,其中包含已添加指定时间间隔的日期和时间值。
             */
            private void btn_AddM_Click(object sender, EventArgs e)
            {
                G_datetime = DateAndTime.DateAdd(//向时间字段中添加一分钟
                    DateInterval.Minute, 1, G_datetime);
                lab_time.Text = G_datetime.ToString(//显示时间信息
                    "时间:  yyyy年M月d日 H时m分s秒");
            }
            private void btn_AddH_Click(object sender, EventArgs e)
            {
                G_datetime = DateAndTime.DateAdd(//向时间字段中添加一小时
                    DateInterval.Hour, 1, G_datetime);
                lab_time.Text = G_datetime.ToString(//显示时间信息
                    "时间:  yyyy年M月d日 H时m分s秒");
            }
            private void btn_addD_Click(object sender, EventArgs e)
            {
                G_datetime = DateAndTime.DateAdd(//向时间字段中添加一天
                  DateInterval.Day, 1, G_datetime);
                lab_time.Text = G_datetime.ToString(//显示时间信息
                    "时间:  yyyy年M月d日 H时m分s秒");
            }
        }
    }

    三:使用TimeSpan对象获取时间间隔,截图

    四:代码

    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 GetInterval
    {
        public partial class Frm_Main : Form
        {
            public Frm_Main()
            {
                InitializeComponent();
            }
    
            private DateTime G_DateTime_First,//定义两个时间字段
                G_DateTime_Second;
            private void btn_First_Click(object sender, EventArgs e)
            {
                G_DateTime_First = DateTime.Now;//为时间字段赋值
                lab_first.Text = "系统时间:" +//显示时间
                    G_DateTime_First.ToString(
                    "yyyy年M月d日 H时m分s秒 fff毫秒");
            }
            private void btn_Second_Click(object sender, EventArgs e)
            {
                G_DateTime_Second = DateTime.Now;//为时间字段赋值
                lab_second.Text = "系统时间:" +//显示时间
                    G_DateTime_Second.ToString(
                    "yyyy年M月d日 H时m分s秒 fff毫秒");
            }
            private void btn_Result_Click(object sender, EventArgs e)
            {
                TimeSpan P_timespan_temp =//将新的 TimeSpan 结构初始化为指定的天数、小时数、分钟数、秒数和毫秒数。计算两个时间的时间间隔
                    G_DateTime_First > G_DateTime_Second ?
                    G_DateTime_First - G_DateTime_Second :
                    G_DateTime_Second - G_DateTime_First;
                lab_result.Text = string.Format(//显示时间间隔
                    "间隔时间:{0}天{1}时{2}分{3}秒 {4}毫秒",
                    P_timespan_temp.Days, P_timespan_temp.Hours,//P_timespan_temp.Days获取当前 TimeSpan 结构所表示的时间间隔的天数部分。
                    P_timespan_temp.Minutes, P_timespan_temp.Seconds,
                    P_timespan_temp.Milliseconds);
            }
        }
    }
  • 相关阅读:
    吉特仓储管系统(开源WMS)--分享两月如何做到10W+的项目
    吉特仓库管理系统(开源)-如何在网页端启动WinForm 程序
    RequireJS中的require如何返回模块
    RequireJS shim 用法说明
    从ISTIO熔断说起-轻舟网关熔断
    数据库与数据仓库的区别是什么
    API是什么
    要想业务中台建得快,最好用Service Mesh来带
    中台建设之路-中台建设怎么做?建设中台需要具备什么?
    为什么要建设中台
  • 原文地址:https://www.cnblogs.com/hongmaju/p/3765359.html
Copyright © 2011-2022 走看看