How to find days
between two dates in c#.net
DateTime dt1
= new DateTime(2011,
01, 01);
DateTime dt2
= new DateTime(2011,
01, 20);
int
ireturn = (int)dt2.Subtract(dt1).TotalDays;
(or)
var
elapsedDays = (endDate - initialDate).TotalDays;
(or)
DateTime
start = DateTime.Now;
DateTime end
= DateTime.Now.AddDays(5);
TimeSpan
span = end.Subtract(start);
(or)
DateTime dt1
= Convert.ToDateTime(Textbox1.text);
DateTime dt2
= Convert.ToDateTime(Textbox1.text);
TimeSpan ts
= dt2.subtract(dt1);
(or)
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="HeaderContent"
runat="server"
ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent"
runat="server"
ContentPlaceHolderID="MainContent">
<asp:Label ID="Label9" runat="server">
</asp:Label>
<asp:Label ID="Label10" runat="server">
</asp:Label><br />
<br />
<asp:Label ID="Label1" runat="server">
</asp:Label><br />
<asp:Label ID="Label2" runat="server">
</asp:Label><br />
<asp:Label ID="Label3" runat="server">
</asp:Label><br />
<asp:Label ID="Label4" runat="server">
</asp:Label><br />
<asp:Label ID="Label5" runat="server">
</asp:Label><br />
<asp:Label ID="Label6" runat="server">
</asp:Label><br />
<asp:Label ID="Label7" runat="server">
</asp:Label><br />
<asp:Label ID="Label8" runat="server">
</asp:Label><br />
</asp:Content>
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object
sender, EventArgs e)
{
DateTime
startTime = Convert.ToDateTime("12 Aug 2012 12:30 PM");
DateTime
endTime = Convert.ToDateTime("11 Sep 2013 15:30 PM");
Label9.Text = "StartTime
:" + startTime;
Label10.Text = "EndTime
:" + endTime;
//We subtract
the time.
TimeSpan
span = endTime.Subtract(startTime);
//This will
just return the difference selected option
Label1.Text = "Time
Difference (seconds): " + span.Seconds;
Label2.Text = "Time
Difference (minutes): " + span.Minutes;
Label3.Text = "Time
Difference (hours): " + span.Hours ;
Label4.Text = "Time
Difference (days): " + span.Days ;
//This will
return the total difference based on selected options
Label5.Text = "Total
milliseconds: " + Math.Round(span.TotalSeconds,
2).ToString() ;
Label6.Text = "Total
Minutes: " + Math.Round(span.TotalMinutes,
2).ToString() ;
Label7.Text = "Total
hours: " + Math.Round(span.TotalHours,
2).ToString();
Label8.Text = "Total
days: " + Math.Round(span.TotalDays,
2).ToString();
}
}
No comments:
Post a Comment