Link to home
Start Free TrialLog in
Avatar of Sheldon Livingston
Sheldon LivingstonFlag for United States of America

asked on

Need to pull the day portion from a date.

I am using EPPLUS to populate Excel.

If, using C#, I MessageShow the contents of a cell that looks like "12-Feb" it will display "2/12/2019 12:00:00 AM".  So, this looks like a date.

Now I want to throw into a variable the day portion of that cell... the 12.

Any idea on the conversion needed here?
Avatar of Bill Prew
Bill Prew

I haven't worked with EPPLUS, but if that cell presents as a datetime, then you should just be able to use it's .Day method to get the day number.

If it presents as a string then you would want to convert it to a datetime, then take the .Day of that.


»bp
If it presents as an Int, then I saw this example (link below for reference) that talks about EPPLUS dates...  Basically looks like:

  1. c# - Excel Date column returning INT using EPPlus - Stack Overflow

long dateNum = long.Parse(worksheet.Cells[row, column].Value.ToString());
DateTime result = DateTime.FromOADate(dateNum);

Open in new window


»bp
Avatar of Sheldon Livingston

ASKER

When I MessageBox the GetType.ToString() it states System.DateTime yet I cannot figure out how to grab the day.

I'm using EPPLUS within C#.

I MessageBox myWS.Cells["F4"].Value.ToString() and it shows the date and time.
Just keeps going in loops stating can't convert one thing to another.

The code below doesn't show errors until you run it.  Then you get "System.FormatException: 'Input string was not in a correct format.'"
Long dateValue = long.Parse(myWS.Cells["F4"].Value.ToString());
DateTime result = DateTime.FromOADate(dateValue);
MessageBox.Show(result.Day.ToString());

Open in new window

This works as a test here, so I expect it's the first statement.  I would debug and see what myWS.Cells["F4"].Value and myWS.Cells["F4"] are typed as and how they look.  Once you can get the Excel date into long then it should work.

long dateValue = 40000;
DateTime result = DateTime.FromOADate(dateValue);
Console.WriteLine(result.Day.ToString());

Open in new window


»bp
This is ridiculous.  I ended up just using another cell in Excel to read the Day of the cell above it.

Now I want to compare that cell contents with a date from a database and am having the same issue.  No way to pull the day only from the database field that is a date.

Next option is to develop a method to convert the database field into a string and then parse out the slashes to get to the day portion of the date.

This is crazy.
Bill... I've tried several iterations of trying to convert the cell contents to long as well... to no avail.
If you can hold off till tomorrow I'll pull down EPPLUS and play around with it and see what I can discover.


»bp
Amazing:

This line:
string someString = new string(myRecordSet.Fields["finvdate"].Value);
Yields this error:  
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'The best overloaded method match for 'string.String(char*)' has some invalid arguments'

This line:
string someString = new string(myRecordSet.Fields["finvdate"].Value.ToString());
Yields this error:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'The best overloaded method match for 'string.String(char*)' has some invalid arguments'

I'm thinking that maybe there isn't a way to convert a date into anything?
Thanks Bill... I've posted another question just to see if it is possible to convert a date to a string.
ASKER CERTIFIED SOLUTION
Avatar of Sheldon Livingston
Sheldon Livingston
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>I got help on converting the date to a string and then just parsed it out to find the day.

Aha - see my comment in your other question.  You can get the day directly from the date.  This approach just gives you extra work.
Thank you all!