i wanted to convert 26/Jun/2016 at 13:14
from a string
to datetime
data type
for saving to sql
string DtTime = ds.Tables["VOUCHER"].Rows[0]["BASICDATETIMEOFINVOICE"].ToString();
how to split as a string
i wanted to convert 26/Jun/2016 at 13:14
from a string
to datetime
data type
for saving to sql
string DtTime = ds.Tables["VOUCHER"].Rows[0]["BASICDATETIMEOFINVOICE"].ToString();
how to split as a string
You need to escape the /
and the at
in the format string, then you can use ParseExact
:
DateTime.ParseExact("26/Jun/2016 at 13:14", "dd/MMM/yyyy 'at' HH:mm", CultureInfo.InvariantCulture);
https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings
But are you sure that the value is not stored as DateTime
? Check by using ds.Tables["VOUCHER"].Rows[0].Field<DateTime>("BASICDATETIMEOFINVOICE")
. Then no conversion from Object
to string
to DateTime
is needed. If not you should consider to store it as DateTime
in the first place, wherever the DataTable
was filled from.