Reputation: 940
I'm trying to get DateTime
value from an uploaded excel file to the databse. This is what I have:
DateTime date = DateTime.Now;
if(worksheet.Cells[row, 1].Value != null){//if cell is not empty
date = worksheet.Cells[row, 1].Value.ToString().Trim();//doesn't work b/c not the same type
//I also tried:
//date = DateTime.FromOAdDate(worksheet.Cells[row, 1].Value)
}
The above expects a type double
for FromOADate
. I'm not sure how to get the cell value to convert to type double
.
Any suggestions would be greatly appreciated!
Upvotes: 0
Views: 2195
Reputation: 11364
You need to parse the string to Datetime.
date = DateTime.Parse(worksheet.Cells[row, 1].Value.ToString());
you can also use the DateTime.TryParse method to make sure you get a value and not an exception.
DateTime.TryParse(worksheet.Cells[row, 1].Value.ToString(), out date)
Upvotes: 1