Reputation: 463
I have a string that has minutes.seconds.milliseconds. I need to convert this into a SQL Server compliant value of type TIME
.
This is what I've tried so far but I'm still getting format exceptions
var a = "1.39.26";
var ab = a.Split('.');
var minutes = ab[0];
var seconds = ab[1];
var ms = ab[2];
string time = minutes + ":" + seconds + "." + ms;
var one = TimeSpan.ParseExact(time, "mm:ss.FF",CultureInfo.InvariantCulture);
var two = DateTime.ParseExact(time, "mm:ss.FF",
CultureInfo.InvariantCulture
);
Also tried
string time = DateTime.Now.Date.ToString() + minutes + ":" + seconds + "." + ms;
var one = TimeSpan.ParseExact(time, "dd-MM-yyyy mm:ss.FF", CultureInfo.InvariantCulture);
var two = DateTime.ParseExact(time, "dd-MM-yyyy mm:ss.FF", CultureInfo.InvariantCulture);
Upvotes: 2
Views: 492
Reputation: 98810
We can't know that what your DateTime.Now.Date.ToString()
returns since it depends on your CurrentCulture
settings but you can directly parse this string to TimeSpan
as;
var a = "1.39.26";
TimeSpan ts = TimeSpan.ParseExact(a, "m\\.ss\\.ff", CultureInfo.InvariantCulture);
Later than you can add this value to your DateTime.Today
if you want.
DateTime dt = DateTime.Today.Add(ts);
But since TIME
mapped with TimeSpan
in CLR side, I doubt that you want this.
Further reading;
Upvotes: 5