ASP.NET - Days Calculation from date

Asked By Rahul
31-Jan-12 08:21 AM
how to calculate days month and year for given from date to todate ?
  Suchit shah replied to Rahul
31-Jan-12 08:36 AM
That depends on what you want to calculate exactly.


You can't translate the value in a TimeSpan to exact years and months, as the length of years and months varies. You can calculate approximate years and months like this:


int years = ts.Days / 365;
int months = (ts.Days % 365) / 31;
If you want the exact difference, you have to compare the DateTime values.

have a look on 
http://www.codeproject.com/Articles/44048/Calculate-Difference-Between-Two-Dates-in-Day-Week 
http://www.codeproject.com/Articles/28837/Calculating-Duration-Between-Two-Dates-in-Years-Mo 
  Suchit shah replied to Rahul
31-Jan-12 08:41 AM
below example will show you  how to Calculate number of years, months and days between two given dates in asp.net using c#
protected void Page_Load(object sender, EventArgs e) 
   
   
   
DateTime dt1 = Convert.ToDateTime("02/01/2011"); 
   
DateTime dt2 = Convert.ToDateTime("11/04/2010"); 
 //Initializing object and then calling constructor 
   
CalculateDateDifference dateDiff = new CalculateDateDifference(dt1,dt2); 
   
int totalMonths = dateDiff.Months; 
int totalDays = dateDiff.Days; 
int totalYears= dateDiff.Years; 
//Class that will calculate the number of days, months and years between two given dates. 
  
public class CalculateDateDifference 
  private int[] monthDay = new int[12] { 31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; 
  private DateTime fromDate; 
  private DateTime toDate; 
  private int year; 
  private int month; 
  private int day; 
   
   
//Public type Constructor 
   
public CalculateDateDifference(DateTime d1, DateTime d2) 
   
   
int increment; 
//To Date must be greater 
   
if (d1 > d2) 
   
   
this.fromDate = d2; 
 this.toDate = d1; 
   
 else 
   
   
this.fromDate = d1; 
   
this.toDate = d2; 
   
   
/// Day Calculation  
increment = 0; 
   
   
if (this.fromDate.Day > this.toDate.Day) 
   
   
increment = this.monthDay[this.fromDate.Month - 1]; 
   
   
   
/// if it is february month 
   
/// if it's to day is less then from day 
   
if (increment == -1) 
   
   
if (DateTime.IsLeapYear(this.fromDate.Year)) 
   
   
// leap year february contain 29 days 
   
increment = 29; 
   
   
else 
   
   
increment = 28; 
   
   
   
if (increment != 0) 
   
   
day = (this.toDate.Day + increment) - this.fromDate.Day; 
   
increment = 1; 
   
   
else 
   
   
day = this.toDate.Day - this.fromDate.Day; 
   
   
///month calculation 
  
if ((this.fromDate.Month + increment) > this.toDate.Month) 
   
   
this.month = (this.toDate.Month + 12) - (this.fromDate.Month + increment); 
   
increment = 1; 
   
   
else 
   
   
this.month = (this.toDate.Month) - (this.fromDate.Month + increment); 
   
increment = 0; 
   
   
   
/// 
   
/// year calculation 
   
/// 
   
this.year = this.toDate.Year - (this.fromDate.Year + increment); 
   
   
   
   
public override string ToString() 
   
   
//return base.ToString(); 
   
return this.year + " Year(s), " + this.month + " month(s), " + this.day + " day(s)"
   
   
   
public int Years 
   
   
get 
   
   
return this.year; 
   
   
   
   
public int Months 
   
   
get 
   
   
return this.month; 
   
   
   
   
public int Days 
get 
return this.day; 
}
  D Company replied to Rahul
31-Jan-12 08:43 AM
Hello Friend.

System.DataTime.Now returns the current date and time and you can do the formating of date and time using different functions. Type a dot after Now and looks for the options available.

System.DataTime.Now.Day will return no of days

let me know if you want to know some other things related to this


Regards
D
  dipa ahuja replied to Rahul
31-Jan-12 11:04 AM
 void countDays()
  {
 
  DateTime d1 = DateTime.Parse(txtFrom.Text);
  DateTime d2 = DateTime.Parse(txtTo.Text);
  TimeSpan span = d2 - d1;
  Console.WriteLine
     ("There're {0} days between {1} and {2}", span.TotalDays, d1.ToString(), d2.ToString());
  }
 
 
  Sandeep Mittal replied to Rahul
31-Jan-12 11:50 AM
private string DateDiff(DateTime fromdate, DateTime todate)
{
  TimeSpan ts;
  int Years=0, Months=0, Days=0;
  //string returnString;
  DateTime newDate;
  newDate = fromdate;
  while (newDate <= todate)
  {
    Years++;
    newDate = newDate.AddYears(1);
  }
  Years--;
  fromdate = fromdate.AddYears(Years);
  newDate = fromdate;
  while (newDate <= todate)
  {
    Months++;
    newDate = newDate.AddMonths(1);
  }
  Months--;
  fromdate = fromdate.AddMonths(Months);
  ts = todate.Subtract(fromdate);
  Days = ts.Days;
  return Years.ToString() + "years," + Months.ToString() + " months," + Days.ToString() + " days";    
}
  Rahul replied to dipa ahuja
01-Feb-12 02:53 AM
thank u for ur coding. ur coding is very nice and simple
  dipa ahuja replied to Rahul
01-Feb-12 03:01 AM
Your Welcome !
Create New Account
help
alot var Hi What are Master Pages in ASP.NET? or What is a Master Page? ASP.NET master pages allow you to create a consistent layout for the pages in your application. A single master page defines the look and feel and standard behavior that you want for all of the you want to display. When users request the content pages, they merge with the master page to produce output that combines the layout of the master page with the content from the content page. What are the 2 important parts of a master page? The following are the 2 important parts of a master page 1. The Master Page itself 2. One or more Content Pages Can Master Pages be nested? Yes, Master Pages
Page Directive? What is the purpose of page directive in aspx page? Directive Syntax Directives are instructions used to specify settings (related to how a page should render and processed) used by the page and user control compilers when they process ASP.NET Web Forms page (.aspx) and user control (.ascx) files. These are the essential part of every ASP.NET Page or Control. Directives can be located anywhere in an .aspx or .ascx file, but the values, same as any HTML tag) that are specific to that directive. Special Note: The @ Page directive can be used only in .aspx files, and the @ Control directive can be used
this.tabControl1.ResumeLayout(false); this.ResumeLayout(false); this.PerformLayout(); } private void tab1Button1_Click(object sender, System.EventArgs e) { / / Inserts the code that should run when the button is clicked. } void tabControl1_Selected(object e.TabPage)) { _cache.Remove(e.TabPage); } _cache.Add(e.TabPage); } } private void button1_Click(object sender, EventArgs e) { TabPage page = new TabPage("TabPage" + pageNumber.ToString()); AddControlsToTabPage(page); this.tabControl1.TabPages.Add(page); pageNumber++; this.tabControl1.SelectedTab = page; } private void AddControlsToTabPage(TabPage page) { page.Controls.Add(this.tab1CheckBox1); page.Controls.Add(this.tabCheckBox2); page.Controls.Add(this.tabCheckBox3); page
renders the same css differently. Very odd. Sir also some other features : in our profile page we are not able to see the symbol of answer unchecked, checked , ignored, 3* , 1 just missed that feature. Sir, now most of things are working fine as expected ! Profile page , My Post Page etc working good ! site looks great ! Sir one thing i notice . . Every posts has the today's Date like DateTime.Now So i think the date field is not getting fetched and not showing the asp-net / 17 / 10371909 / gridview-sorting-using-jquery.aspx Navigation menu missing in forum merit page http: / / www.eggheadcafe.com / forummerit.aspx Regards Hi Robbe, I suggest you that, if you hi. . previously there is dropdownlist in which we used to select number of posts per page . . . but in the New site it is missing. . . . hi. . . check out the belowlines 2 Replies each Answer posted Hi Robbe, After giving replay for the question and submitting the button, page is showing the top of the page. My suggestion is if the page shows the answer which we have replied wiil be
while creating an ASP.NET application? There are two level of asp.net debugging 1. Page level debugging For this we have to edit the page level debugging enable the trace to true in the line in the html format of the page. %@ Page Language = ”vb” trace = ”true”AutoEventWireup = ”false” Codebehind = ”WebForm1.aspx.vb” Inherits = ”WebApplication2.WebForm1?&gt; 2 Enable trace enabled = true. If there is a calendar control to be included in each page of your application, and and we do not intend to use the Microsoft-provided calendar do you develop it? Do you copy and paste the code into each and every page of your application? Create the Calendar User Control The control we will create will contain cookies: Session cookies Persistent cookies Tell few steps for optimizing (for speed and resource) ASP page / application. Avoid mixing html code with asp code Which command using Query Analyzer will give an ASP.NET Web application. • 15. What is the property available to check if the page posted or not? • Ans : The Page_Load event handler in the page checks for IsPostBack property