문제

I have a string which represents a date, its given back from a DropDownList. The string is "27.08.2010" for example. Now I want to add the current time to this and parse it to Datetime ... so in the end it should be a DateTime like 27.08.2010 15:12:45 for example.

How could I do this? Right now, I am putting together a string using DateTime.Now.Hour etc. and make a DateTime out of it, but that seems to be the wrong way.

Thanks :)

도움이 되었습니까?

해결책

 string s = "27.08.2010";
 DateTime dt = DateTime.ParseExact(s, "dd.MM.yyyy", CultureInfo.InvariantCulture);
 DateTime result = dt.Add(DateTime.Now.TimeOfDay);

다른 팁

You can parse the string into a DateTime instance then just add DateTime.Now.TimeOfDay to that DateTime instance.

  DateTime date = DateTime.ParseExact("27.08.2010", "dd.MM.yyyy", System.Globalization.CultureInfo.InvariantCulture);
  TimeSpan time = DateTime.Now.TimeOfDay;
  DateTime datetime = date + time;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top