Renato Fonseca
Renato Fonseca

Reputation: 25

How to initialize a date time variable in MVC

I have an SQL table with a timestamp colum that has this value: '2016-03-19 15:58:38.967'

how do i declare a datetime variable in MVC to have exactly the same value as the value mentioned above in the sql table? Ty

Upvotes: 1

Views: 1351

Answers (1)

eyebleach
eyebleach

Reputation: 338

Use ParseExact and ".fff" for microseconds.

string sqlDate = "2014-05-12 18:30:18.182";
var date = DateTime.ParseExact(sqlDate, "yyyy-MM-dd HH:mm:ss.fff", System.Globalization.CultureInfo.InvariantCulture);

Format spec: https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx

Upvotes: 1

Related Questions