Calculate number of years, months and days between two given dates in asp.net using c#

In this tutorial, you will learn how to calculate number of days, months and years between two given dates in asp.net using c#. Asp.net has built-in class TimeSpan. Using TimeSpan class you can calculate no of days, hours, minutes, seconds and even milliseconds but if you want to calculate total no of months and years between two given dates then TimeSpan class cannot help you. So that's why the focus of this article is to calculate exact number of days, months and years between two given dates using custom class given below. Let's have a look over it.

Calculate number of years, months and days between two given dates in asp.net using c#

Calculate-days-months-years.aspx.cs
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

{

/// <summary>

/// defining Number of days in month; index 0 represents january and 11 represents December

/// february contain either 28 or 29 days, so here its value is -1

/// which will be calculate later.

/// </summary>

private int[] monthDay = new int[12] { 31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };


/// <summary>

/// contain from date

/// </summary>

private DateTime fromDate;


/// <summary>

/// contain To Date

/// </summary>

private DateTime toDate;


/// <summary>

/// these three variable of integer type for output representation..

/// </summary>

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;

}

}


}
Now totalDays variable will have total number of days between these given dates, likewise totalMonthstotalYears variable will have total number of years between two given dates. So this is the way to calculate number of years, months and days between two given dates in asp.net using c#. variable will have total number of months and
I hope it will be a great helpful for you. 
Happy Coding, Keep Coding.
Read more...

Learn how to find number of days in a given month in asp.net using c#

In this tutorial you will learn how to find number of days in a given month in asp.net using c#. It's quite easy. DateTime class has built-in function DaysInMonth(year,month) that takes two parameters, first one is Year, second one is month and then return the number of days in that given month. Let’s have a look over example given below

Find number of days in a given month in asp.net using c#

Number-of-days-in-month.aspx.cs
int totalDays = DateTime.DaysInMonth(2007, 6); //Pass valid year and month

Now totalDays variable will have total number of days in a given month. I hope it will be great helpful for you.


Happy Coding, Keep Coding.
Read more...

Learn how to find whether given year is Leap year or not in asp.net using c#

In this tutorial of asp.net you will learn how to find whether given year is Leap year or not using c#. It's quite simple in asp.net by using the built-in IsLeapYear(Year) method of DateTime class that take one parameter which is a valid year. Let's have a look over example given below

Find whether given year is Leap year or not in asp.net using c#

Find-leap-year.aspx.cs
bool isLeapYear= DateTime.IsLeapYear(2007);

In the example given above, it will return True to the isLeapYear variable of Boolean type. I hope it will be great helpful for you.


Happy Coding, Keep Coding.
Read more...

avoid jquery conflict with other javascript libraries

In this jQuery tutorial you will learn how to avoid jquery conflict with other javascript libraries. jQuery is a powerful javascript library. Using jQuery with other javascript libraries such as YUI, prototype or mootools, you may found conflict because they all use $ as their main function name and as a result jquery code will not run. To resolve this issue, jQuery has .noConflict() method. Let's have a look over the example given below


Avoid jquery conflict with other javascript libraries


<html>
<head>
<script src="prototype.js"></script>
<script src="jquery.js"></script>
<script language="javascript" type="text/javascript">
jQuery.noConflict();
// Using jQuery via jQuery(...)
jQuery(document).ready(function(){
jQuery("someid").hide();
});


// Using Prototype with $(...), etc.
$('someid').hide();
</script>
</head>
<body>Use of .noConflict() method of jQuery</body>
</html>



It will revert $ back to its original library.


Additionally, there is another option for you. If you want to make sure that jQuery won't conflict with other javascript libraries - but you want the benefit of a short name, you can do something like i have done below:

<html>
<head>
<script src="prototype.js"></script>
<script src="jquery.js"></script>
<script language="javascript" type="text/javascript">
var $hi = jQuery.noConflict();
// Using jQuery via $hi (...)
$hi(document).ready(function(){
//don't write other javascript libraries code in this region
$hi("div").hide();
});


// Using Prototype with $(...), etc.
$('someid').hide();
</script>
</head>
<body></body>
</html>

You can define anything as your own alternate name (e.g. hello, pakistan, cricket, jq).


Finally, if you don't want to define alternative to the jQuery name (you deadly want to use $ and don't care about using another library's $ method), then you have another solution. This is most frequently used in such case when you still want the benefits of really short jQuery code, but you don't want to cause conflicts with other javascript libraries.

<html>
<head>
<script src="prototype.js"></script>
<script src="jquery.js"></script>
<script language="javascript" type="text/javascript">
jQuery.noConflict();


// Put all your jquery code in your document ready area
jQuery(document).ready(function($){
// don't write other javascript libraries code in this region
$("someid").hide();
});


// Using Prototype with $(...), etc.
$('someid').hide();
</script>
</head>
<body></body>
</html>

This is probably the perfect solution for most of your code, considering that there'll be less code that you'll have to change for getting complete compatibility of jquery with other javascript libraries. So these are the different techniques to avoid jquery conflict with other javascript libraries.

Read more...