Here is a table

enter image description here

The dt_send_daytime column indicates the time in the format dd: hh: mm: ss. Where dd is the number of the day of the week. If name_ru = hour, then you need to take only the time in the format mm: ss . And if name_ru = Week, then only the day of the week.

How to separate the day of the week from time?

  • dd is the day of the week? - MaximK
  • Isn't it easier to keep the day of the week separate from the time? - MaximK
  • yes this is the day of the week - propro17
  • it's just that it’s written on a database, I have to solve it ( - propro17

2 answers 2

A string of this type as you have recorded in the database is not recognized as DateTime. It is necessary to take it apart. For this, there is a string.Split () method. Get the array. Zero item will be the day of the week.

string dt_str = row["dt_send_datetime"]; string[] dt_str_tmp = st_str.Split(':'); switch (row["name_ru"]) { case "Час": // Можно сразу так. В итоге получим к примеру 4:0 Console.WriteLine(dt_str_tmp[2]+":"+dt_str_tmp[3]); // Можно конвертировать во время // DateTime dt = DateTime.Parse(string.Format("{0}:{1}:{2}", dt_str_tmp[1], dt_str_tmp[2], dt_str_tmp[3])); // и потом вывести // Console.WriteLine(dt.ToString("mm:ss"); // В итоге получим 04:00 break; case "Неделя": Console.WriteLine(dt_str_tmp[0]); break; } 

You can replace the whole switch , provided that the choice of only two values ​​- "hour" or "week", with:

Console.WriteLine((name_ru == "Час")?dt_str_tmp[2]+":"+dt_str_tmp[3]:dt_str_tmp[0]);

  • instead of ("mm: ss") you can write (tmp [1]: tmp [2]) ??? - propro17
  • not really. corrected the answer - MaximK
  • The error 'row ["dt_send_datetime"]', this is my object and not the string - propro17
  • And just row["dt_send_datetime"].ToString() not rolling? - MaximK
  • and now on (row ["name_ru"]) - propro17

From the database, get the full date, cast the DateTime type and use the ToString () method. For example string dt = DateTime.Now.ToString("HH:mm") // выведет текущую дату в формате ЧЧ:ММ

Specifically in your case

 string dt; if (row["name_ru"] == "Час") { dt = row["dt_send_daytime"].ToString("mm:ss"); } else if (row["name_ru"] == "Неделя") { dt = row["dt_send_daytime"].ToString("dd"); } 
  • A string of such format as that of the author will not be recognized as DateTime - MaximK
  • such a solution does not work so it turns out ??? - propro17
  • Try it. An error will occur. - MaximK