Do you need to find the difference in number of days, hours or even minute between the two date range?

Here's how:

Assuming the a and b variable is of type DateTime object.

DateTime a = DateTime.Now;
DateTime b = DateTime.Now
(b - a).TotalDays

Example:

namespace WindowsFormsApplication1

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

this.dateTimePicker1.CustomFormat = "MM/dd/yyyy HH:mm";

this.dateTimePicker1.Format = System.Windows.Forms.DateTimePickerFormat.Custom;

}

private void button1_Click(object sender, EventArgs e)

{

//Calculate the total number of days

textBox1.Text = (dateTimePicker2.Value - dateTimePicker1.Value).TotalDays.ToString("#");

//Calculate the total number of hours

textBox2.Text = (dateTimePicker2.Value - dateTimePicker1.Value).TotalHours.ToString("#");

//Other options are total of minutes, seconds and milliseconds.

}

}

}

How to Calculate difference between two dates in C# z的更多相关文章

  1. Calculate difference between consecutive data points in a column from a file

    cat > temp0015101269125  awk 'p{print $0-p}{p=$0}' temp00152-633-7 REF: https://www.unix.com/shel ...

  2. Java 两个日期间的天数计算

    在Java中计算两个日期间的天数,大致有2种方法:一是使用原生JDK进行计算,在JDK8中提供了更为直接和完善的方法:二是使用第三方库. 1.使用原生的JDK private static long  ...

  3. 46 个非常有用的 PHP 代码片段

    在编写代码的时候有个神奇的工具总是好的!下面这里收集了 40+ PHP 代码片段,可以帮助你开发 PHP 项目. 这些 PHP 片段对于 PHP 初学者也非常有帮助,非常容易学习,让我们开始学习吧- ...

  4. 56个PHP开发常用代码

    2016/02/14 6203 4    在编写代码的时候有个神奇的工具总是好的!下面这里收集了 50+ PHP 代码片段,可以帮助你开发 PHP 项目. 这些 PHP 片段对于 PHP 初学者也非常 ...

  5. [转]Date and String Function in BluePrism

    本文转自:https://www.codeproject.com/Articles/1247389/Date-and-String-Function-in-BluePrism This article ...

  6. php 常用的自定义函数

    1. 发送 SMS 在开发 Web 或者移动应用的时候,经常会遇到需要发送 SMS 给用户,或者因为登录原因,或者是为了发送信息.下面的 PHP 代码就实现了发送 SMS 的功能. 为了使用任何的语言 ...

  7. PHP实用代码片段(三)

    1. 目录清单 使用下面的 PHP 代码片段可以在一个目录中列出所有文件和文件夹. function list_files($dir) { if(is_dir($dir)) { if($handle ...

  8. 45 Useful Oracle Queries--ref

    http://viralpatel.net/blogs/useful-oracle-queries/ Here’s a list of 40+ Useful Oracle queries that e ...

  9. 【转】46 个非常有用的 PHP 代码片段

    1. 发送 SMS 在开发 Web 或者移动应用的时候,经常会遇到需要发送 SMS 给用户,或者因为登录原因,或者是为了发送信息.下面的 PHP 代码就实现了发送 SMS 的功能. 为了使用任何的语言 ...

随机推荐

  1. C#接口的经典案例

    C#接口(interface)实例子(简单而经典)2008/12/04 10:04using System; using System.Collections.Generic; using Syste ...

  2. git init 与 git init --bare 的区别

    git init  和 git init –bare 的区别 使用命令"git init --bare"(bare汉语意思是:裸,裸的)初始化的版本库(暂且称为bare repos ...

  3. Linux 按行分割文件(转载)

    将一个大文件分成若干个小文件方法 例如将一个BLM.txt文件分成前缀为 BLM_ 的1000个小文件,后缀为系数形式,且后缀为4位数字形式 先利用 wc -l BLM.txt       读出 BL ...

  4. 【转】Wireshark:“There are no interfaces on which a capture can be done ”

    linux环境下 两种解决方案:    第一种方法:使用root用户登陆        xiaoshancun@xiaoshancun-VM500:~$ sudo wireshark    第二种方法 ...

  5. 欧拉工程第57题:Square root convergents

    题目链接 Java程序 package projecteuler51to60; import java.math.BigInteger; import java.util.Iterator; impo ...

  6. Spring3 报org.aopalliance.intercept.MethodInterceptor问题解决方法

    原文:Spring3 报org.aopalliance.intercept.MethodInterceptor问题解决方法 一 开发环境:JDK5+Spring3.0.5+Myeclipse6.6+T ...

  7. 使用 Spring 3 来创建 RESTful Web Services(转)

    使用 Spring 3 来创建 RESTful Web Services 在 Java™ 中,您可以使用以下几种方法来创建 RESTful Web Service:使用 JSR 311(311)及其参 ...

  8. 61. Rotate List

    题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example:Giv ...

  9. java对称加密(AES)

    java对称加密(AES) 博客分类: Java javaAES对称加密  /** * AESHelper.java * cn.com.songjy.test * * Function: TODO * ...

  10. Intellij IDEA新建一个EJB工程(二)

    从博文:http://www.cnblogs.com/yangyquin/p/5328344.html 中可以知道如何利用Intellij IDEA建立一个EJB Module,还有新建一个测试Mod ...