Frage

I need to use

SqlDateTime.Parse(val)

where val is a string such as " 23.3.1992 00:00:00 ".

The string is in European format, that is, day precedes month. However Parse wants "American" format. How I can tell it to use particular datetime format / locale?

Thanks in advance!

War es hilfreich?

Lösung

Try this:

string val = "23.12.1992 00:00:00";

// Parse exactly from your input string to the native date format.
DateTime dt = DateTime.ParseExact(val, "dd.M.yyyy hh:mm:ss", null);

// Part to SqlDateTime then            
System.Data.SqlTypes.SqlDateTime dtSql = System.Data.SqlTypes.SqlDateTime.Parse(dt.ToString("yyyy/MM/dd"));

This could be done in one statement, but just separated for illustration.

Andere Tipps

Have you tried DateTime instead of SQLDateTime

DateTime d = DateTime.Parse(val); 
String s = d.ToString(CultureInfo.CreateSpecificCulture("en-US"));

Can you try this ?

string valInEuropean = "23.3.1992 00:00:00";
DateTime dateInEuropean = DateTime.Parse(valInEuropean);
string valInAmerican = dateInEuropean.ToString("yyyy-MM-dd HH:mm:ww");

For converting a string to datetime object when the format is known(in this case ) use

DateTime dwweek = DateTime.ParseExact("23.3.1992 00:00:00", "dd.MM.yyyy hh:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top