Sheldon Livingston
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?
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?
If it presents as an Int, then I saw this example (link below for reference) that talks about EPPLUS dates... Basically looks like:
»bp
long dateNum = long.Parse(worksheet.Cells[row, column].Value.ToString());
DateTime result = DateTime.FromOADate(dateNum);
»bp
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.ToS tring() and it shows the date and time.
I'm using EPPLUS within C#.
I MessageBox myWS.Cells["F4"].Value.ToS
ASKER
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.'"
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());
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.
»bp
long dateValue = 40000;
DateTime result = DateTime.FromOADate(dateValue);
Console.WriteLine(result.Day.ToString());
»bp
ASKER
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.
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.
ASKER
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
»bp
ASKER
Amazing:
This line:
string someString = new string(myRecordSet.Fields[ "finvdate" ].Value);
Yields this error:
Microsoft.CSharp.RuntimeBi nder.Runti meBinderEx ception: 'The best overloaded method match for 'string.String(char*)' has some invalid arguments'
This line:
string someString = new string(myRecordSet.Fields[ "finvdate" ].Value.To String());
Yields this error:
Microsoft.CSharp.RuntimeBi nder.Runti meBinderEx ception: '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?
This line:
string someString = new string(myRecordSet.Fields[
Yields this error:
Microsoft.CSharp.RuntimeBi
This line:
string someString = new string(myRecordSet.Fields[
Yields this error:
Microsoft.CSharp.RuntimeBi
I'm thinking that maybe there isn't a way to convert a date into anything?
ASKER
Thanks Bill... I've posted another question just to see if it is possible to convert a date to a string.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>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.
Aha - see my comment in your other question. You can get the day directly from the date. This approach just gives you extra work.
ASKER
Thank you all!
If it presents as a string then you would want to convert it to a datetime, then take the .Day of that.
»bp