Pages

Friday, January 1, 2010

All Sundays between two dates in asp.net C#

Find Sundays between two dates in asp.net C# .
Introduction : In this article i will show you how to get all Sundays between two dates in asp.net c# .
Following is function i have written for getting all Sundays between two dates in c# .It is very just you need to add code and you will able get all Sundays between two dates in c#

protected void Page_Load(object sender, EventArgs e)
{
    DateTime Dt1 = DateTime.Now;
    DateTime Dt2 = DateTime.Now.AddDays(30);
    GetAllSundays(Dt1, Dt2);
}
public void GetAllSundays(DateTime Date1, DateTime Date2)
{
    TimeSpan DateDiff = Date2.Subtract(Date1);
    for (int i = 0; i <= DateDiff.Days; i++)
    {
        if (Date1.Date.AddDays(i).DayOfWeek == DayOfWeek.Sunday)
        {
         Response.Write(Date1.Date.AddDays(i).ToLongDateString() + " " + Date1.Date.AddDays(i).DayOfWeek + "
"); } } }

Related Other posts


Find week number from Date in asp.ne c#

Find the week from the date in asp.net c# :

Introduction : This article describe you how to find week number from date in c# .I have created one function which return you week number from the date in c#  .

C# Code for finding the week number :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HamidSite
{
    public partial class Test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {            
            int WeekNo = GetWeeknumber(DateTime.Now);
        }
        public int GetWeeknumber(DateTime Date)
        {
            try
            {   int iMonth = Date.Month;
                int returnNumber = 0;
                for (int i = 1; i <= 5; i++)
                {
                    if (iMonth == Date.AddDays(-7 * i).Month)
                    { }
                    else
                    {
                        returnNumber = i; break;
                    }
                }
                return returnNumber;  
            }
            catch (Exception Exc) { throw Exc; }
        }
    }
}

Related Other posts