Please help with converting a Date and a Time string after concate

G

Guest

I have 2 strings, "12/28/2005" and "16:44:54". I need to conatenate them and
convert them to datetime format so I can assign them to a datetime field in a
datarow. The time is now being converted to the date "12/28/2005". and I
don't know how to concatenate them since "+" won't work after I convert them.
Thanks, Alpha

if (result == "00")
{
char[] separator = {','};
char[] intSep = {'.'};
string lastDate, lastTime, lastMilleage;
DataRow drVeh = dsVehicle.Tables["VehDetail"].Rows[VListing.CurrentRowIndex];
DialogResult msgRet;

string[] replySeparated = ATSReply.Split(separator);
lastDate = replySeparated[2];
lastTime = replySeparated[3];
lastMilleage = replySeparated[4];
lastMilleage = lastMilleage.Substring(0, lastMilleage.Length - 3);
string[] intLastMilleage = lastMilleage.Split(intSep);

msgRet = MessageBox.Show("Would you like to update the vehicle odometer to
" + lastMilleage +
" and the last odometer read date to " + lastDate + " and the read time to
" +
lastTime, "Live Update", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if(msgRet == DialogResult.Yes)
{
DateTime DatelastDate = Convert.ToDateTime(lastDate);
DateTime TimeLastTime = Convert.ToDateTime(lastTime);
// DateTime lastDateTime = DatelastDate + TimeLastTime;

drVeh["LastOdometerDate"] = DatelastDate;
drVeh["LastOdometerReading"] = Convert.ToInt32(intLastMilleage[0]);
drVeh.EndEdit();

}

}
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

You could use DateTime.ParseExact( date_String + " " + time_String,
"MM/dd/yyyy HH:mm:ss, CultureInfo.InvariantCulture.DateTimeFormat );
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


Sorry for the re-post ,check the DateTimeFormatInfo class for build format
strings
 
G

Guest

Thank you Ignacio. That worked out great!

Alpha

Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

You could use DateTime.ParseExact( date_String + " " + time_String,
"MM/dd/yyyy HH:mm:ss, CultureInfo.InvariantCulture.DateTimeFormat );


--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Alpha said:
I have 2 strings, "12/28/2005" and "16:44:54". I need to conatenate them
and
convert them to datetime format so I can assign them to a datetime field
in a
datarow. The time is now being converted to the date "12/28/2005". and I
don't know how to concatenate them since "+" won't work after I convert
them.
Thanks, Alpha

if (result == "00")
{
char[] separator = {','};
char[] intSep = {'.'};
string lastDate, lastTime, lastMilleage;
DataRow drVeh =
dsVehicle.Tables["VehDetail"].Rows[VListing.CurrentRowIndex];
DialogResult msgRet;

string[] replySeparated = ATSReply.Split(separator);
lastDate = replySeparated[2];
lastTime = replySeparated[3];
lastMilleage = replySeparated[4];
lastMilleage = lastMilleage.Substring(0, lastMilleage.Length - 3);
string[] intLastMilleage = lastMilleage.Split(intSep);

msgRet = MessageBox.Show("Would you like to update the vehicle odometer to
" + lastMilleage +
" and the last odometer read date to " + lastDate + " and the read time to
" +
lastTime, "Live Update", MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
if(msgRet == DialogResult.Yes)
{
DateTime DatelastDate = Convert.ToDateTime(lastDate);
DateTime TimeLastTime = Convert.ToDateTime(lastTime);
// DateTime lastDateTime = DatelastDate + TimeLastTime;

drVeh["LastOdometerDate"] = DatelastDate;
drVeh["LastOdometerReading"] = Convert.ToInt32(intLastMilleage[0]);
drVeh.EndEdit();

}

}
 
Top