-5

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

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
fidha
  • 21
  • 6
  • [Convert.ToDateTime()](https://msdn.microsoft.com/en-us//library/xhz1w05e(v=vs.110).aspx?f=255&MSPPError=-2147217396) – waka Sep 14 '17 at 09:10
  • All of these questions about converting `string` to `DateTime` and vice-versa should be merged into one answer. Without that there will still be a bunch of duplicates. – mrogal.ski Sep 14 '17 at 09:13
  • it showing error-@Tim Schmelter – fidha Sep 14 '17 at 09:18
  • @m.rogalski: maybe, but this answer would fill a book because there are many edge cases and format specifier. It should at least contain the documentation which is mainly [this](https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings) and [this](https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings). So i doubt that the answer would be very helpful if someone searches for help because `DateTime.ParseExact("26/Jun/2016 at 13:14", "dd/MMM/yyyy at HH:mm", null)` doesn't work – Tim Schmelter Sep 14 '17 at 09:33

1 Answers1

0

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.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • how to remove escape the / and the at from this.plz help me – fidha Sep 14 '17 at 09:28
  • @fidha: i dont understand your question – Tim Schmelter Sep 14 '17 at 09:31
  • string[] Date = DtTime.Split(' '); string[] bal = Date[0].Split('-'); DateTime dt = DateTime.Parse(bal[0]); string sty = dt.ToString("yyyy/MM/dd"); int month = dt.Month; string[] time = Date[2].Split(':');how can i save to a string by merging all to "2017-09-14 02:29:29" this formate – fidha Sep 14 '17 at 09:34
  • @fidha: why don't you use my approach? – Tim Schmelter Sep 14 '17 at 09:35
  • I'd feel more comfortable with `CultureInfo.InvariantCulture` instead of `null` (or is `"Jun"` always recognized as the 6th month? Assuming `null` would cause a fallback to `Current(UI)Culture`), but other than that, this works. – Corak Sep 14 '17 at 09:35
  • @corak: you're right. Then i can also remove the escape characters around `/` which is only needed if you use `CurrentCulture` (or `null`). – Tim Schmelter Sep 14 '17 at 09:36
  • ... and then `ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture)` (which is what OP seems to want to end up with) – Corak Sep 14 '17 at 09:39
  • thanks-@TimSchmelter – fidha Sep 14 '17 at 09:41
  • @fidha: i really don't understand your approach. If the string contains `at` then my approach works, test it with your sample string. You don't need to split it, that's very error prone. If you need the date use `dateTime.Date`. If you need the time use `dateTime.TimeOfDay`. If you need the hour use `dateTime.TimeOfDay.Hours`. If you need the minute use `dateTime.TimeOfDay.Minutes`, and so on. You see, all you need is to parse the string to `DateTime` with the correct format. – Tim Schmelter Sep 14 '17 at 09:45
  • 26/Jun/2016 at 13:14 ."at" is on the xml file .i am not adding – fidha Sep 14 '17 at 11:07