0

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

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Mandragorasprout
  • 123
  • 1
  • 6
  • 12

7 Answers7

4

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);
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
ybo
  • 16,967
  • 2
  • 28
  • 31
  • To make absolutely sure you get no time-of-day component from the first line (the conversion of the text string), you could add a `.Date` to the end of the first line. – Jeppe Stig Nielsen Feb 20 '13 at 08:58
2

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;
dotNET
  • 33,414
  • 24
  • 162
  • 251
0
date.Hour=DateTime.Now.Hour;
date.Minute=DateTime.Now.Minute;
date.Second=DateTime.Now.Second;
  • 3
    that's a rather poor answer why are you doing 3 steps when you can do it in one step..? – MethodMan Feb 20 '13 at 07:58
  • And this is relevant with this question because..? – Soner Gönül Feb 20 '13 at 07:59
  • First, you shouldn't read the `Now` property more than one time. The clock may change, so if it's `09:59:59` when you read the first part, and `10:00:00` when you read the last part, you might get a mix like `09:00:00` which is far from the true "now". Also if your `date` identifier is supposed to be a `DateTime` value, you can mutate it like you do. The properties in question are `get`-only (no setters). Downvoted. – Jeppe Stig Nielsen Feb 20 '13 at 09:03
0

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);
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Vandana
  • 161
  • 7
0

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;
         }
Pankaj Nagarsekar
  • 219
  • 1
  • 6
  • 17
  • 1
    Don't use `"MM"` for minutes; it's _months_. It wasn't the purpose to get the date part of `Now`. It really isn't necessary to use `ToString` here. – Jeppe Stig Nielsen Feb 20 '13 at 09:10
-1
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);
gregjer
  • 2,823
  • 2
  • 19
  • 18
-1

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);
            }
        }
Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
MevlütÖzdemir
  • 3,180
  • 1
  • 23
  • 28