将回历日期作为字符串转换为 DateTime 对象
Convert Hijri date as string to a DateTime object
我想要的是将文本转换成回历日期。
我尝试了下面的代码,但是问题没有解决
Dim iString As String = "١٤٤٢/٠٨/٠٨ ٢:٣٨ ص"
Dim myDate As DateTime = CDate(iString)
Dim myDatea As DateTime = DateTime.ParseExact(iString, "yyyy-MM-dd HH:mm tt", Nothing)
Dim myDateb As DateTime = DateTime.Parse(iString)
Dim dta As DateTime = DateTime.ParseExact(iString, "dd/MM/yyyy hh:mm:ss tt", CultureInfo.GetCultureInfo("ar-sa"))
Dim dtb As DateTime = DateTime.Parse(iString).ToString("dd-MM-yyyy HH:mm:ss tt")
Dim culture As CultureInfo = New CultureInfo("ar-sa")
Dim tempDate As DateTime = Convert.ToDateTime(iString, culture)
MsgBox(tempDate)
在我看来,您需要将阿拉伯数字转换为 0-9 才能将其解析为 DateTime。
然后,当您将它作为 DateTime 时,您可以使用 Hirji 日历将其显示为 Hirji 日期:
Public Shared Function GetDateTimeFromArabicNumerals(s As String) As DateTime
' DateTime.Parse may not handle am/pm in Arabic, so check and make the adjustment later
Dim pmOffset = If(s.IndexOf("م") >= 0, 12, 0)
s = s.Replace("م", "").Replace("ص", "")
Dim t = ""
For Each c In s
Dim a = AscW(c)
If a >= 1632 AndAlso a <= 1641 Then
t &= Char.GetNumericValue(c).ToString()
Else
t &= c
End If
Next
Dim ci As CultureInfo = New CultureInfo("ar-SA")
Dim hijri As Calendar = New HijriCalendar()
ci.DateTimeFormat.Calendar = hijri
Dim df = "yyyy/MM/dd h:mm"
Dim dts = DateTimeStyles.AllowWhiteSpaces
Dim dt = DateTime.ParseExact(t, df, ci, dts).AddHours(pmOffset)
Return dt
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim s As String = "١٤٤٢/٠٨/٠٨ ٢:٣٨ ص"
Dim dt = GetDateTimeFromArabicNumerals(s)
Dim ci As CultureInfo = New CultureInfo("ar-SA")
Dim hijri As Calendar = New HijriCalendar()
ci.DateTimeFormat.Calendar = hijri
MsgBox(dt.ToString(ci))
End Sub
输出:
你需要检查一下,因为我不懂阿拉伯语,所以我可能对“p.m”使用了错误的文本,或者实际上它可能给出了完全错误的日期。
参考文献:
- Convert a datetime string in Unicode (with Arabic numerals) to DateTime object
- Work with calendars
未正确解析阿拉伯数字。
您可以将数值替换为 其他 阿拉伯符号(1、2、3、...),并将 ص/م
指示符(如果有)替换为 AM/PM
替代形式:
Dim nativeDate As String = "١٤٤٢/٠٨/٠٨ ٢:٣٨ ص"
Dim hijriDate = ParseHijriDate(nativeDate, False)
Dim gregorianDate = ParseHijriDate(nativeDate, True)
其中分别打印:
8/8/1442 2:38:00 AM
3/21/2021 2:38:00 AM
Private Function ParseHijriDate(HijriDate As String, AsGregorianDate As Boolean) As Date
Dim culture = CultureInfo.CreateSpecificCulture("ar-SA")
Dim arSADigitsMap = GetCultureDigitsMap(culture)
For Each map In arSADigitsMap
HijriDate = HijriDate.Replace(map.Key, map.Value)
Next
If AsGregorianDate Then
Return Date.Parse(HijriDate, culture)
Else
Return Date.Parse(HijriDate, CultureInfo.InvariantCulture)
End If
End Function
Private Function GetCultureDigitsMap(culture As CultureInfo) As Dictionary(Of String, String)
Dim digits = culture.NumberFormat.NativeDigits
Dim digitsMap = New Dictionary(Of String, String)()
For n As Integer = 0 To 9
digitsMap.Add(digits(n), n.ToString())
Next
digitsMap.Add(culture.DateTimeFormat.AMDesignator, "AM")
digitsMap.Add(culture.DateTimeFormat.PMDesignator, "PM")
Return digitsMap
End Function
如果您想强制使用特定格式 (yyyy/MM/dd H:mm tt
),请将格式添加到 DateTime.ParseExact()
:
Return Date.ParseExact(HijriDate, "yyyy/MM/dd H:mm tt", CultureInfo.InvariantCulture)
注:
如果用于映射数字的字典无法实现结果,您可以使用预建字典,它以两种形式提供数字的静态表示:
Dim arSAEastWest = New Dictionary(Of String, String)() From {
{"٠", "0"}, {"١", "1"}, {"٢", "2"}, {"٣", "3"}, {"٤", "4"},
{"٥", "5"}, {"٦", "6"}, {"٧", "7"}, {"٨", "8"}, {"٩", "9"},
{"ص", "AM"}, {"م", "PM"}
}
我想要的是将文本转换成回历日期。
我尝试了下面的代码,但是问题没有解决
Dim iString As String = "١٤٤٢/٠٨/٠٨ ٢:٣٨ ص"
Dim myDate As DateTime = CDate(iString)
Dim myDatea As DateTime = DateTime.ParseExact(iString, "yyyy-MM-dd HH:mm tt", Nothing)
Dim myDateb As DateTime = DateTime.Parse(iString)
Dim dta As DateTime = DateTime.ParseExact(iString, "dd/MM/yyyy hh:mm:ss tt", CultureInfo.GetCultureInfo("ar-sa"))
Dim dtb As DateTime = DateTime.Parse(iString).ToString("dd-MM-yyyy HH:mm:ss tt")
Dim culture As CultureInfo = New CultureInfo("ar-sa")
Dim tempDate As DateTime = Convert.ToDateTime(iString, culture)
MsgBox(tempDate)
在我看来,您需要将阿拉伯数字转换为 0-9 才能将其解析为 DateTime。
然后,当您将它作为 DateTime 时,您可以使用 Hirji 日历将其显示为 Hirji 日期:
Public Shared Function GetDateTimeFromArabicNumerals(s As String) As DateTime
' DateTime.Parse may not handle am/pm in Arabic, so check and make the adjustment later
Dim pmOffset = If(s.IndexOf("م") >= 0, 12, 0)
s = s.Replace("م", "").Replace("ص", "")
Dim t = ""
For Each c In s
Dim a = AscW(c)
If a >= 1632 AndAlso a <= 1641 Then
t &= Char.GetNumericValue(c).ToString()
Else
t &= c
End If
Next
Dim ci As CultureInfo = New CultureInfo("ar-SA")
Dim hijri As Calendar = New HijriCalendar()
ci.DateTimeFormat.Calendar = hijri
Dim df = "yyyy/MM/dd h:mm"
Dim dts = DateTimeStyles.AllowWhiteSpaces
Dim dt = DateTime.ParseExact(t, df, ci, dts).AddHours(pmOffset)
Return dt
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim s As String = "١٤٤٢/٠٨/٠٨ ٢:٣٨ ص"
Dim dt = GetDateTimeFromArabicNumerals(s)
Dim ci As CultureInfo = New CultureInfo("ar-SA")
Dim hijri As Calendar = New HijriCalendar()
ci.DateTimeFormat.Calendar = hijri
MsgBox(dt.ToString(ci))
End Sub
输出:
你需要检查一下,因为我不懂阿拉伯语,所以我可能对“p.m”使用了错误的文本,或者实际上它可能给出了完全错误的日期。
参考文献:
- Convert a datetime string in Unicode (with Arabic numerals) to DateTime object
- Work with calendars
未正确解析阿拉伯数字。
您可以将数值替换为 其他 阿拉伯符号(1、2、3、...),并将 ص/م
指示符(如果有)替换为 AM/PM
替代形式:
Dim nativeDate As String = "١٤٤٢/٠٨/٠٨ ٢:٣٨ ص"
Dim hijriDate = ParseHijriDate(nativeDate, False)
Dim gregorianDate = ParseHijriDate(nativeDate, True)
其中分别打印:
8/8/1442 2:38:00 AM
3/21/2021 2:38:00 AM
Private Function ParseHijriDate(HijriDate As String, AsGregorianDate As Boolean) As Date
Dim culture = CultureInfo.CreateSpecificCulture("ar-SA")
Dim arSADigitsMap = GetCultureDigitsMap(culture)
For Each map In arSADigitsMap
HijriDate = HijriDate.Replace(map.Key, map.Value)
Next
If AsGregorianDate Then
Return Date.Parse(HijriDate, culture)
Else
Return Date.Parse(HijriDate, CultureInfo.InvariantCulture)
End If
End Function
Private Function GetCultureDigitsMap(culture As CultureInfo) As Dictionary(Of String, String)
Dim digits = culture.NumberFormat.NativeDigits
Dim digitsMap = New Dictionary(Of String, String)()
For n As Integer = 0 To 9
digitsMap.Add(digits(n), n.ToString())
Next
digitsMap.Add(culture.DateTimeFormat.AMDesignator, "AM")
digitsMap.Add(culture.DateTimeFormat.PMDesignator, "PM")
Return digitsMap
End Function
如果您想强制使用特定格式 (yyyy/MM/dd H:mm tt
),请将格式添加到 DateTime.ParseExact()
:
Return Date.ParseExact(HijriDate, "yyyy/MM/dd H:mm tt", CultureInfo.InvariantCulture)
注:
如果用于映射数字的字典无法实现结果,您可以使用预建字典,它以两种形式提供数字的静态表示:
Dim arSAEastWest = New Dictionary(Of String, String)() From {
{"٠", "0"}, {"١", "1"}, {"٢", "2"}, {"٣", "3"}, {"٤", "4"},
{"٥", "5"}, {"٦", "6"}, {"٧", "7"}, {"٨", "8"}, {"٩", "9"},
{"ص", "AM"}, {"م", "PM"}
}