Change date display with DateTimePicker

AMER SAID 396 Reputation points
2021-08-02T10:37:03.36+00:00

hi

Welcome

The following tool DateTimePicker takes the Date of the device.
I want if the date of the device is Hijri, the date will be shown in the tool DateTimePicker with the Gregorian date.

I have tried all of the following methods to bypass the device date and ignore it if it is not AD. There is no result.

'//1
       Dim Ar As CultureInfo = New CultureInfo("ar-eg") '
       Thread.CurrentThread.CurrentCulture = Ar
       Ar.DateTimeFormat.Calendar = New GregorianCalendar

'/// 2
       Dim ci As CultureInfo = New CultureInfo("ar-eg")
       Dim info As DateTimeFormatInfo = ci.DateTimeFormat
       DateTimePicker1.Format = System.Windows.Forms.DateTimePickerFormat.Custom
       DateTimePicker1.CustomFormat = info.ShortDatePattern
'/// 3
 Function Tomiladeg(ByVal gDate As Date, Optional ByVal format As String = Nothing) As String
       Return gDate.ToString(format, New Globalization.CultureInfo("ar-EG"))
   End Function

 DateTimePicker1.Value = Convert.ToDateTime(Tomiladeg(Now, "dd-MM-yyyy"))
Developer technologies | VB

Locked Question. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

1 answer

Sort by: Most helpful
  1. Riyadh Prayer 0 Reputation points
    2026-11-01T05:40:15.0566667+00:00

    The key point is that a DateTime value itself does not have a calendar. The Hijri or Gregorian display is only how that same instant is formatted, and the WinForms DateTimePicker formats its value using the thread's current culture. So do not convert the date to a string and parse it back — that round trip just reinterprets it under the current culture, which is why attempt 3 did not work.

    Instead, set a cloned culture whose calendar is Gregorian before the control is shown, and give the picker an explicit custom format. Put this in your startup code, or in the form's Load event before you set the value:

    Dim gregorianCulture As CultureInfo = CType(CultureInfo.GetCultureInfo("en-US").Clone(), CultureInfo)

    gregorianCulture.DateTimeFormat.Calendar = New GregorianCalendar()

    Thread.CurrentThread.CurrentCulture = gregorianCulture

    Thread.CurrentThread.CurrentUICulture = gregorianCulture

    DateTimePicker1.Format = DateTimePickerFormat.Custom

    DateTimePicker1.CustomFormat = "dd-MM-yyyy"

    DateTimePicker1.Value = DateTime.Today

    If you want to keep Arabic text but still show Gregorian dates, clone "ar-SA" (or "ar-EG") instead of "en-US" and replace only its calendar in the same way:

    Dim arabicCulture As CultureInfo = CType(CultureInfo.GetCultureInfo("ar-SA").Clone(), CultureInfo)

    arabicCulture.DateTimeFormat.Calendar = New GregorianCalendar()

    Thread.CurrentThread.CurrentCulture = arabicCulture

    Thread.CurrentThread.CurrentUICulture = arabicCulture

    Two details matter. First, changing CurrentCulture after the DateTimePicker has already been created may not change its display, so set the culture as early as possible. Second, assign a Date value directly, as above. Do not do DateTimePicker1.Value = Convert.ToDateTime(someString), because the string parsing step can bring the device calendar back into the result.

    With the Gregorian calendar in the thread culture and a custom format, the picker will show the Gregorian date even when the device calendar is Hijri.

    Was this answer helpful?

    0 comments No comments