C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






Comparing and Adding Dates


Posted Date: 30 Apr 2008    Resource Type: Code Snippets    Category: Date & Time

Posted By: komaladevi       Member Level: Gold
Rating:     Points: 20



<%@ Page Language="C#" %>

<script runat="server">
public void Page_Load(Object sender, EventArgs e){

//create the new dates: one by using the property Now
//and another using the DateTime constructor of Year,Month,Day

System.DateTime date1 = System.DateTime.Now;
System.DateTime date2 = new System.DateTime(1972,8,21);

//Show the dates
Response.Write("<hr>");
Response.Write("Show current dates");
Response.Write("<hr>");
Response.Write("date1: " + date1 + "<br>");
Response.Write("date2: " + date2 + "<P>");
Response.Write("Show Date Difference: ");

//Compare the dates
if (DateTime.Compare(date1, date2) > 0) Response.Write("date1 > date2");
if (DateTime.Compare(date1, date2) == 0) Response.Write("date1 == date2");
if (DateTime.Compare(date1, date2) < 0) Response.Write("date1 < date2");

//Add to the second date using AddYears to add 40 years to the date
System.DateTime date2new = date2.AddYears(40);

//Show the dates again this time with the new date2
Response.Write("<p>");
Response.Write("<hr>");
Response.Write("Show new dates with new date 2");
Response.Write("<hr>");
Response.Write("date1: " + date1 + "<br>");
Response.Write("date2new: " + date2new + "<P>");
Response.Write("Show new Date Difference: ");

//Now compare the dates
if (DateTime.Compare(date1, date2new) > 0) Response.Write("date1 > date2new");
if (DateTime.Compare(date1, date2new) == 0) Response.Write("date1 == date2new");
if (DateTime.Compare(date1, date2new) < 0) Response.Write("date1 < date2new");

}
</script>

Attachments

  • Comparing and Adding Dates (15144-3083-Page Language.doc)



  • Responses

    Author: komaladevi    30 Apr 2008Member Level: Gold   Points : 0



    Author: k prabhakar varma    05 May 2008Member Level: Gold   Points : 2
    it is fine for comparing two dates whether greater,lesser and equal.
    But What about Actual scenario Date Difference,i.e. the difference in years,or months, or weeks,or days should be caleculated and be displayed between two Dates.


    Author: shaik mohiuddin    07 May 2008Member Level: Gold   Points : 2
    gud one for intermediate level


    Feedbacks      
    Popular Tags   What are tags ?   Search Tags  
    (No tags found.)

    Post Feedback


    This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
    You must Sign In to post a response.
    Next Resource: Get the System Date , using c#
    Previous Resource: How to compare two different times using VB.NET
    Return to Discussion Resource Index
    Post New Resource
    Category: Date & Time


    Post resources and earn money!
     
    Related Resources



    dotNet Slackers   BizTalk Adaptors    Web Design

    doors in nj

    Contact Us    Privacy Policy    Terms Of Use