Re: Zeitzonen und Benutzerdefinierte Formate --- Problem



"Manfred Büttner" <manbuettner@xxxxxxxxxxx> schrieb
Hallo,
nachdem ich meine Software umgestellt habe, damit sowohl in
Deutschland und USA gleichermaßen die Datumsabfrage funktioniert,
die als Ausgangsbasis aber in Deutsch sein muss, habe ich Probleme,
die Benutzerspezifischen Formate abzufangen und wäre über einen Tipp
sehr froh.

Stehen die Regions und Sprachoptionen auf Datum, kurz: TT.MM.JJJJ
oder T.M.JJ dann klappt alles wunderbar.
sind diese allerdings auf JJJJ-MM-TT erhalte ich...
"Die Zeichenfolge wurde nicht als gültiges DateTime erkannt."

Wie kann ich dafür sorgen, dass das Datum korrekt Formatiert wird?


sub test
dim INI_MaintenanceDate as String = "25.11.2005"
ConvertDateTimeToCulture(INI_MaintenanceDate,
System.Globalization.CultureInfo.CurrentCulture.Name, "de-DE")
end sub


Public Function ConvertDateTimeToCulture(ByVal sDateTime As String,
_

ByVal NameInputCulture As String, _

ByVal NameOutputCulture As String) As String

Dim InputCulture = New
System.Globalization.CultureInfo(NameInputCulture)

Dim OutputtCulture As CultureInfo = _

CultureInfo.CreateSpecificCulture(NameOutputCulture)

Dim oCurrentCulture As CultureInfo = _

System.Globalization.CultureInfo.CurrentCulture

' Culture des Threads entsprechend des Eingabe-Datums einstellen

Thread.CurrentThread.CurrentCulture = InputCulture

Dim temp As DateTime = DateTime.Parse(sDateTime, _

InputCulture, DateTimeStyles.NoCurrentDateDefault)

Thread.CurrentThread.CurrentCulture = OutputtCulture

' Umgewandelte DateTime als String zurückgeben

ConvertDateTimeToCulture = temp.ToString

ConvertDateTimeToCulture =
DateSerial(Year(ConvertDateTimeToCulture),
Month(ConvertDateTimeToCulture),
Microsoft.VisualBasic.DateAndTime.Day(ConvertDateTimeToCulture))

' Culture des Threads wieder zurückstellen

' (sonst würde das Programm weiter mit der OutputCulture laufen)

Thread.CurrentThread.CurrentCulture = oCurrentCulture

End Function

Ganz habe ich Dein Vorhaben nicht verstanden, aber...
Ich würde erst mal Option Strict aktivieren. Der Rest ergibt sich von
selbst. Wenn man sich Gedanken über die Konvertierungen macht, statt
implizit ungewünschte Konvertierungen zuzulassen, dann behebt das den einen
oder anderen Fehler.

Generell gilt, dass Datumsangaben intern als Date gespeichert werden sollen.
Wenn du wirklich einen Datumsstring von einem Format ins andere konvertieren
willst:

Public Function ConvertDateTimestring( _
ByVal sDateTime As String, _
ByVal NameInputCulture As String, _
ByVal NameOutputCulture As String) As String

Dim inputCulture, outputCulture As CultureInfo
Dim d As Date

inputCulture = New CultureInfo(NameInputCulture)
outputCulture = New CultureInfo(NameOutputCulture)

d = DateTime.Parse(sDateTime, inputCulture.DateTimeFormat)
Return d.ToString(outputCulture.DateTimeFormat)

End Function


Beschreibe aber doch nochmal, wo du mit dem Parsen/Formatieren/Konvertieren ein Problem hast.


Armin

.



Relevant Pages

  • System.FormatException: String was not recognized as a valid DateTime
    ... String was not recognized as a valid DateTime. ... cycles through the tables one by one and outputs the datatables at the end ... Dim objDT1 As DataTable ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Zeitzonen und Benutzerdefinierte Formate --- Problem
    ... "Die Zeichenfolge wurde nicht als gültiges DateTime erkannt." ... Public Function ConvertDateTimeToCulture(ByVal sDateTime As String, ... Dim InputCulture = New ... Thread.CurrentThread.CurrentCulture = InputCulture ...
    (microsoft.public.de.german.entwickler.dotnet.vb)
  • Re: pull dates out of a text file
    ... but I am trying to take a very long string and pull out all the dates in it that are in mm/dd/yyyy format and put them into a collection... ... Private Function ParseDatesAs DateTime() ... Dim validDates As ArrayList = New ArrayList ... ' Convert the ArrayList to an array, cast the array as a DateTime array, ...
    (microsoft.public.dotnet.languages.vb)
  • Re: OnTimer Event Closes Form
    ... Format returns a Variant of String type. ... you actually convert from DateTime ... > Dim dteFileTme As Date ... > Dim strErrorMsg As String ...
    (microsoft.public.access.forms)
  • Zeitzonen und Benutzerdefinierte Formate --- Problem
    ... "Die Zeichenfolge wurde nicht als gültiges DateTime erkannt." ... Public Function ConvertDateTimeToCulture(ByVal sDateTime As String, ... Dim InputCulture = New System.Globalization.CultureInfo ...
    (microsoft.public.de.german.entwickler.dotnet.vb)