Felix JIN
Felix JIN

Reputation: 101

Combine Date and Time from excel into Database

I am trying to have date and time from different columns in excel and write into database. For example in the excel having

Date: 23/01/2019

Time: 18:30:00

The result need read and write will be "2019-01-23 18:30:00" I did google some solutions and tried but still no luck.

Code Sample:

start.DateTime = DateTime.ParseExact(excelReader.GetDateTime(DatePOS) + " " + excelReader.GetDateTime(TimePOS),"yyyy-mm-dd hh:mm:ss",CultureInfo.InvariantCulture);

DatePOS/TimePOS stand for the column position of Date Column and Time Column and I got invalid format error as result. when I check the statement and got

GetDateTime(DatePOS) "23/01/2019 00:00:00"

GetDateTime(TimePOS) "30/12/1899 18:30:00"

start.DateTime "1/01/0001 00:00:00"

Please help, thanks

Upvotes: 0

Views: 249

Answers (1)

Precious Uwhubetine
Precious Uwhubetine

Reputation: 3007

You should be using GetString instead of GetDateTime.The latter returns a parsed date from the excel column . Try this

start.DateTime = DateTime.ParseExact(excelReader.GetString(DatePOS).Split(' ')[0] + " " + excelReader.GetString(TimePOS).Split (' ')[1],
                                     "yyyy-mm-dd hh:mm:ss",
                                     CultureInfo.InvariantCulture);

Upvotes: 2

Related Questions