views:

458

answers:

2

Hi, I have a table in sql where the date format is stored in Hijri. Now I am working on a vb.net application where I have to let the user update that dateField.

So is it possible that if I place a datepicker(which is in Gregorian) and user selects the date and its converts into Hijri date before updating. I mean when the user selects the date and clicks the save button the date should be updated in hijri format in the sql .

For now , the user is entering the date manually on a tms AdvEdit.

Is there any code available to accomplish this task. Thanking you all in advance for your time and consideration.

+1  A: 

The below code will convert to Gregorian/ Hijri according to the parameter passed:

Public Function ConvertDateCalendar(ByVal DateConv As DateTime, ByVal Calendar As String, ByVal DateLangCulture As String) As String

    Dim DTFormat As DateTimeFormatInfo
    DateLangCulture = DateLangCulture.ToLower()
    ''' We can't have the hijri date writen in English. We will get a runtime error - LAITH - 11/13/2005 1:01:45 PM -

    If Calendar = "Hijri" AndAlso DateLangCulture.StartsWith("en-") Then
        DateLangCulture = "ar-sa"
    End If

   ''' Set the date time format to the given culture - LAITH - 11/13/2005 1:04:22 PM -
   DTFormat = New System.Globalization.CultureInfo(DateLangCulture, False).DateTimeFormat

   ''' Set the calendar property of the date time format to the given calendar - LAITH - 11/13/2005 1:04:52 PM -
   Select Case Calendar
       Case "Hijri"
           DTFormat.Calendar = New System.Globalization.HijriCalendar()
           Exit Select

       Case "Gregorian"
           DTFormat.Calendar = New System.Globalization.GregorianCalendar()
           Exit Select
       Case Else

           Return ""
   End Select

   ''' We format the date structure to whatever we want - LAITH - 11/13/2005 1:05:39 PM -
   DTFormat.ShortDatePattern = "dd/MM/yyyy"
   Return (DateConv.[Date].ToString("f", DTFormat))

End Function

Happy coding !!!!!

Ravia
hey ravia , tht was awesome !!!!! Thank you so much !!!
ahmed
A: 

Dear Ahmad, .net Provides a PersianCalendar Class which do this for you.

All you need is to Create an instance from PersianCalendar and use it. All methods are familier Like

 System.Globalization.PersianCalendar pc = new PersianCalendar();
 pc.GetDayOfMonth(YourDate); // and so on

note that All .net Libraries are same and you can use them in each .Net platform language(likeVB, C#, etc) and all of them will compile to CLR, So Just Create an instance and Ride.

I Also Strongly recommand you to store Georgian date to your DB andwhen you want to display the date, Convert it to persian or any other calendar, this let you globalize you application simpler.

And Note that Microsoft Added Hijri Shamsi Calendar to .Net 4 as a type of calendar and you do not need to convert it any more in .net 4.0

GoodLuck

Nasser Hadjloo