Вопрос

I need to combine date and time and insert into one datetime row.

Here is my code:

 DateTime Headlinedate;

 try
 {
     DateTime date = Convert.ToDateTime(txtHeadlinedate.Text.Trim(), Ci);
     string timestr = DateTime.Now.ToString("hh:mm:ss tt");
     DateTime combinedDate = date.Add(TimeSpan.Parse(timestr));
     Headlinedate = combinedDate;

 }
 catch {
     Headlinedate = DateTime.Now;
 }

I'm getting exception :

Input string was not in a correct format

Это было полезно?

Решение

If you get this exception of the first line of code, you'll have to make sure that the content of txtHeadlinedate is valid and parse it accordingly.

If the error comes from the call to TimeSpan.Parse, the following version should fix the problem:

DateTime date = Convert.ToDateTime(txtHeadlinedate.Text.Trim(), Ci);
Headlinedate = date.Add(DateTime.Now.TimeOfDay);

Другие советы

TimeSpan can't parse "tt". Use DateTime to parse it, or instead use HH:mm format to get 24-hour value of hours and remove tt part from your format. Then TimeSpan will be able to parse it. For example:

DateTime date = Convert.ToDateTime(txtHeadlinedate.Text.Trim(), Ci);
string timestr = DateTime.Now.ToString("HH:mm:ss");
DateTime combinedDate = date.Add(TimeSpan.Parse(timestr));
Headlinedate = combinedDate;
date.Hour=DateTime.Now.Hour;
date.Minute=DateTime.Now.Minute;
date.Second=DateTime.Now.Second;

I guess this should work

DateTime Headlinedate;
try
         {
             DateTime date = Convert.ToDateTime(txtHeadlinedate.Text.Trim(), Ci);
             string timestr = DateTime.Now.ToString("MM/dd/yyyy HH:MM:ss");// here is the solution
             DateTime combinedDate = date.Add(TimeSpan.Parse(timestr));
             Headlinedate = combinedDate;

         }
         catch {
             Headlinedate = DateTime.Now;
         }

Use DateTime.TryParse and pass your txtHeadlinedate.Text.Trim() value

DateTime.TryParse(String, IFormatProvider, DateTimeStyles, DateTime)

And for adding timespan value either use

DateTime myDate = ExistingTime.Date.AddHours(hh).AddMinutes(min);

or // Calculate what day of the week is 36 days from this instant.(like below)

System.DateTime today = System.DateTime.Now; System.TimeSpan duration = new System.TimeSpan(36, 0, 0, 0); System.DateTime answer = today.Add(duration);
DateTime date = Convert.ToDateTime(txtHeadlinedate.Text.Trim(), Ci);    
DateTime combinedDate = new DateTime(date.Year, date.Month, date.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);

This works!

DateTime _dateOnly = DateTime.Now;
DateTime _timeOnly = DateTime.Now;

public DateTime Combined { get; set; }

      public DateTime DateOnly
        {
            get { return _dateOnly.Date; }
            set
            {
                _dateOnly = value.Date;
                Combined = DateOnly.Add(TimeOnly.TimeOfDay);
            }
        }

        public DateTime TimeOnly
        {
            get { return _timeOnly.ToLocalTime(); }
            set
            {
                _timeOnly = value.ToLocalTime();
                Combined = DateOnly.Add(TimeOnly.TimeOfDay);
            }
        }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top