Search code examples
c#asp.netdate-conversion

Convert From Hijri to Georgian date in ASP & C#?


I have two text boxes, One in Hijri Date which I am inserting date in this text box using the calender extender. What I want is that upon selection of date (Hijri Date), the Georgian text box will filled with converted Georgian date using C#.

Does anyone has code for changing the hijri date to georgian date?

enter image description here


Solution

  • You can simply convert between Hijri and Gregorian using the built in CultureInfo class

    Imports System.Globalization
    
    Private Sub Convert_From_Hijri_To_Gregorian(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
        Dim arCI As CultureInfo = New CultureInfo("ar-SA")
        Dim hijri As String = Me.TextBox1.Text   '//check if string is valid date first
    
        Dim tempDate As DateTime = DateTime.ParseExact(hijri, "dd/MM/yyyy", arCI.DateTimeFormat, DateTimeStyles.AllowInnerWhite)
        Me.TextBox2.Text = tempDate.ToString("dd/MM/yyyy")
    
    End Sub
    

    I am not a C# guy but here is my the conversion of the above code to C#

    using System.Globalization;
    
    private void Convert_From_Hijri_To_Gregorian(System.Object sender, System.EventArgs e)
    {
        CultureInfo arCI = new CultureInfo("ar-SA");
        string hijri = TextBox1.Text;
    
        DateTime tempDate = DateTime.ParseExact(hijri, "dd/MM/yyyy", arCI.DateTimeFormat, DateTimeStyles.AllowInnerWhite);
        TextBox2.Text = tempDate.ToString("dd/MM/yyyy");
    
    
    }
    

    I have written this code after reading this article