It is very easy to format the datetime to string in .NET and this is commonly used by the developers while displaying the date & time in desired format. But if you would like to store the formatted value in datetime variable, the value changes according to the .NET default format.
Default datetime format in .NET is
We will try this to format the value,
When we try to assign the formatted value to a datetime variable the format again changed to default format
To avoid this, use DateTime.ParseExact
DateTime dt = DateTime.ParseExact(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture);
Where is this useful? If you develop client server application the .NET stores the datetime value with timezone. The above syntax is timezone free, which will be useful across the application.
Also refer this link for DateTime best practices http://msdn.microsoft.com/en-us/library/ms973825.aspx