Saturday 16 November 2013

Detecting sequence of at least n sequential/Consecutive Dates from a given List in ASP.NET using C# or VB.NET

Hi friends,in this article i will explain about Detecting sequence of at least n sequential/Consecutive Dates from a given List in ASP.NET using C# or VB.NET.
I already explained about CSS - Gmail-style progress bar when page is loading,ASP.NET AjaxFileUpload Control With Progress Bar using C# or VB.NET,How To Use the AdRotator Control in an ASP.NET Application and  How to Get Previous page URL using JQuery or C#/VB.NET in ASP.NET

ASP.NET:
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Detecting sequence of at least n sequential Dates from a given List in ASP.NET using C#/VB.NET</title>  
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btn" runat="server" Text="cSharp" onclick="btn_Click"  />
    </div>
    </form>
</body>
</html>


In C#.NET:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Test : System.Web.UI.Page
{
    protected void btn_Click(object sender, EventArgs e)
    {
        List<DateTime> dates = new List<DateTime>(){
            new DateTime(2013,4,21),
            new DateTime(2013,4,23),
            new DateTime (2013,4,20),
            new DateTime(2013,4,14),
             new DateTime(2013,4,24),
             new DateTime(2013,4,18)
        };
        DateTime vacationDateToCheck = new DateTime(2013,4,22);
        DateTime expectedDate = vacationDateToCheck.AddDays(1);
        //if (vacationDateToCheck.DayOfWeek == DayOfWeek.Friday)
        expectedDate = expectedDate.AddDays(2);
        DateTime startDate = vacationDateToCheck;
        DateTime endDate = vacationDateToCheck;
        dates.Add(vacationDateToCheck);
        dates.Sort();
        int count = 0;
        int n = 4;      
        for (int i=0; i < dates.Count; i++)
        {
          
            if ((i > 0) && (i<dates.Count-1))
            {
  if (((dates[i] - dates[i - 1]).TotalDays == 1) && ((dates[i+1] - dates[i ]).TotalDays == 1)_ && dates[i]==vacationDateToCheck)
                {
                    count = count + 1;
                    for (int k = 0; k < dates.Count ; k++)
                        {
                            if (i + k+1  < dates.Count)
                            {
                            
                            if (((dates[i + k + 1] - dates[i]).TotalDays == k+1))
                            {
                              
                              count = count + 1;
                            }
                            }
                        }
                    for (int s = 0; s < dates.Count ; s++)
                    {
                     
                            if (i - s  > 0)
                            {
                               
                                if (((dates[i] - dates[i - s - 1]).TotalDays == s+1))
                                {
                                    
                                    count = count + 1;
                                }
                            }
                        }
                    }
                }
            }
        Response.Write(count);
         
        }

}


In VB.NET:
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data
Imports System.Data.SqlClient

Partial Public Class Test
    Inherits System.Web.UI.Page
    Protected Sub btn_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim dates As New List(Of DateTime)() From { _
         New DateTime(2013, 4, 21), _
         New DateTime(2013, 4, 23), _
         New DateTime(2013, 4, 20), _
         New DateTime(2013, 4, 14), _
         New DateTime(2013, 4, 24), _
         New DateTime(2013, 4, 18) _
        }
        Dim vacationDateToCheck As New DateTime(2013, 4, 22)
        Dim expectedDate As DateTime = vacationDateToCheck.AddDays(1)
        'if (vacationDateToCheck.DayOfWeek == DayOfWeek.Friday)
        expectedDate = expectedDate.AddDays(2)
        Dim startDate As DateTime = vacationDateToCheck
        Dim endDate As DateTime = vacationDateToCheck
        dates.Add(vacationDateToCheck)
        dates.Sort()
        Dim count As Integer = 0
        Dim n As Integer = 4
        For i As Integer = 0 To dates.Count - 1

            If (i > 0) AndAlso (i < dates.Count - 1) Then
                If ((dates(i) - dates(i - 1)).TotalDays = 1) AndAlso ((dates(i + 1) - dates(i)).TotalDays = 1) AndAlso dates(i) = vacationDateToCheck Then
                    count = count + 1
                    For k As Integer = 0 To dates.Count - 1
                        If i + k + 1 < dates.Count Then

                            If ((dates(i + k + 1) - dates(i)).TotalDays = k + 1) Then

                                count = count + 1
                            End If
                        End If
                    Next
                    For s As Integer = 0 To dates.Count - 1

                        If i - s > 0 Then

                            If ((dates(i) - dates(i - s - 1)).TotalDays = s + 1) Then

                                count = count + 1
                            End If
                        End If
                    Next
                End If
            End If
        Next
        Response.Write(count)

    End Sub

End Class



The output of the above code as shown in the below figure.


"If you like my blog or articles, you can appreciate by leaving your comments or Liking my Facebook pageAspdotnet-kishore, following on Google+ Aspdotnet-Kishore, Twitter  on AspdotnetKishore, Linked in Aspdotnet-Kishore, stumbling my posts on stumble upon and subscribing on  RSSfeed Aspdotnet-Kishore for free updates directly to your Email inbox . Watch my blog  for more articles."

No comments:

Post a Comment

© 2012-2018 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.