Archive

Archive for March, 2011

Get Age from BirthDate in Days and Months and Years

March 25, 2011 Leave a comment

Hi all,

try this example to Calculate age from BirthDate in Days and Months and Years

We will use this C# Method which take BirthDate as Parameter and Returns String of Number of Days and Months and Years

private string GetAge(DateTime birthday)
    {
        DateTime Now = DateTime.Now;
        int Years = new DateTime(DateTime.Now.Subtract(birthday).Ticks).Year - 1;
        DateTime dtPastYearDate = birthday.AddYears(Years);
        int Months = 0;
        for (int i = 1; i <= 12; i++)
        {
            if (dtPastYearDate.AddMonths(i) == Now)
            {
                Months = i; break;
            }
            else if (dtPastYearDate.AddMonths(i) >= Now)
            {
                Months = i - 1; break;
            }
        }
        int Days = Now.Subtract(dtPastYearDate.AddMonths(Months)).Days;
        int Hours = Now.Subtract(dtPastYearDate).Hours;
        int Minutes = Now.Subtract(dtPastYearDate).Minutes;
        int Seconds = Now.Subtract(dtPastYearDate).Seconds;
        return String.Format("Age: {0} Year(s) {1} Month(s) {2} Day(s) {3} Hour(s) {4} Second(s)", Years, Months, Days, Hours, Seconds);
    }

and to use this method :-

GetAge(DateTime.Parse("01/21/1984"))

Hope this helps

Good Luck

Advertisements
Categories: ASP.Net

Get Difference between two Dates using SQL Server

March 24, 2011 Leave a comment

Hi all,

try this example to Create Stored Procedure to Get Difference between two Dates in Years , Months and Days

So try this SQL Query to Create Stored Procedure which Take two Parameters (FromDate and ToDate)

create proc CalculateDifference
(
@FromDate datetime,
@ToDate datetime
)
as
begin 
DECLARE @thisYear datetime
DECLARE @years int, @months int, @days int  
SELECT @thisYear = DATEADD(year, DATEDIFF(year, @FromDate, @ToDate), @FromDate) 
SELECT @years = DATEDIFF(year, @FromDate, @ToDate) - (CASE WHEN @thisYear > @ToDate THEN 1 ELSE 0 END) 
SELECT @months = MONTH(@ToDate - @thisYear) - 1 
SELECT @days = DAY(@ToDate - @thisYear) - 1 
SELECT @years as years, @months as months, @days as days
end

and To execute this Stored Procedure write this Query

exec CalculateDifference '01/21/1984','03/24/2011'

Hope this helps,

Good Luck

Categories: SQL Server

Get Last Day of Any Month (Current and Previous and Next)

March 21, 2011 Leave a comment

Hi all

try this example to Get Last Day of Any Month (Current and Previous and Next)

Open SQL Server and write this Query :-

----Last Day of Previous Month
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0))
LastDay_PreviousMonth
----Last Day of Current Month
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0))
LastDay_CurrentMonth
----Last Day of Next Month
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+2,0))
LastDay_NextMonth

If you want to find last day of month of any day specified use this Query :-

--Last Day of Any Month and Year
DECLARE @dtDate DATETIME
SET @dtDate = '8/18/2011'
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@dtDate)+1,0))
LastDay_AnyMonth

Hope this helps

Good Luck

Categories: SQL Server

Convert Hijri Data To Gregorian And Reversal

March 20, 2011 Leave a comment

Hi all,

try this Example to Convert Hijri Data To Gregorian And Reversal using MS SQL Server

** To Convert Hijri Data To Gregorian  we can write this query which will do that :-

select convert(datetime,'22/02/1432',131)

** To Convert from Gregorian Date to Hijri date we can write is sql query :-

select convert(nvarchar(15),getdate(),131)

Hope this helps

GoodLuck

Categories: SQL Server

Printing DIV Contents using Print Preview

March 14, 2011 Leave a comment

Hi all try this example to Print DIV Contents using Print Preview :-


<form id="Form1" method="post" runat="server">
 <div id="Div1">
  Printable content
 </div>
 <input type="button" value="Print" onclick="JavaScript:printPartOfPage('Div1');">

<script type="text/javascript">
<!--
function printPartOfPage(elementId)
{
 var printContent = document.getElementById(elementId);
 var windowUrl = 'about:blank';
 var uniqueName = new Date();
 var windowName = 'Print' + uniqueName.getTime();
 var printWindow = window.open(windowUrl, windowName, 'left=50000,top=50000,width=0,height=0');
 printWindow.document.write(printContent.innerHTML);
 printWindow.document.close();
 printWindow.focus();
 printWindow.print();
 printWindow.close();
}
// -->
</script>
</form>

Hope this helps

Good Luck

Categories: ASP.Net, Javascript

Convert English Number to Arabic Number

March 1, 2011 1 comment

Hi all,

Try this example to Convert English Number to Arabic Number

1) Open VS2005 and create a new Website and add new Web Page

2) From ToolBox add TextBox and Lable and Button Control so our page will be :-

    <div>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <br />
    <asp:Label ID="Label1" runat="server"></asp:Label>
    <br />
    <asp:Button ID="Button1" runat="server" Text="Convert to Arabic" OnClick="Button1_Click" />
    </div>

3) In Code behind add this code :-


public static string TranslateNumerals(string sIn)
    {
        System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
        System.Text.Decoder utf8Decoder = default(System.Text.Decoder);
        utf8Decoder = enc.GetDecoder();
        StringBuilder sTranslated = new System.Text.StringBuilder();
        char[] cTransChar = new char [2];
        byte[] bytes = { 217, 160 };
        // Start Converting characters into Arabic mode.
        char[] aChars = sIn.ToCharArray();
        foreach (char c in aChars)
        {
            if (char.IsDigit(c))
            {
                bytes[1] =byte.Parse( 160 + (int)char.GetNumericValue(c));
                utf8Decoder.GetChars(bytes, 0, 2, cTransChar, 0);
                sTranslated.Append(cTransChar[0]);
            }
            else
            {
                sTranslated.Append(c);
            }
        }
        return sTranslated.ToString();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = "Arabic Number : <b>" + TranslateNumerals(TextBox1.Text.Trim) + "</b>";
    }

Don’t forget to use this Namespace

using System.Text;

Hope this helps

Good Luck.

Categories: ASP.Net