0

I import an excel file into a DataGridView, one of the columns in the excel file is named DATETIME and contains both the date and time, I need to separate the date from the time how can i actually do this

I've tried something but it just gives either the date only or the time only, but I needed it both separately.

Any help on this?

5
Contributors
4
Replies
31
Views
2 Years
Discussion Span
Last Post by pritaeas
0

Assuming the text is in a standard format the DateTime structure has a TryParse method to convert text. The DateTime structure allows you to extract any or all parts of the date and/or the time.

0

If you are absolutely sure that DATETIME string always has the same format, yuo could use something like this:

const int DateStrLength = 5;
string DateTimeStr = "ADateATime";
string DateStr = DateTimeStr.Substring(0, DateStrLength);
string TimeStr = DateTimeStr.Substring(DateStrLength, DateTimeStr.Length - DateStrLength);

Now DateStr will contain the date part "ADate"and TimeStr will contain the time part "ATime".

0

Assuming it's a string; most date-time arrangments have a space as the separator. Try using a split like this:

string datatimeString = "06/02/2015 14:04:00";
var result = datatimeString.Split(' ');
Console.WriteLine("Date: {0}, Time: {1}", result[0], result[1]);

It's simple and gives you both date and time as separate string values:

Console: "Date: 06/02/2015, Time: 14:04:00"

1

Note that Excel might be using an OADate. If that's the case use:

DateTime myDate = DateTime.FromOADate(yourExcelOADate);
Votes + Comments
Keen observation
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.