Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Post Date string conversion to date

I am trying to use the posting date from AR invoices and perform some date calculations on it. But all me attempts get errors. I have found numerous postings here about this subject but nothing is working. Are there any ways to get the date out as a date type? Or is this a VB.net thing that I'm just doing wrong. Code I am using is: (but I have tried many others)

Dim oDocDate2 As SAPbouiCOM.EditText

Dim invoicedate As Date

oItem = oInvForm.Items.Item("10")

oDocDate2 = oItem.Specific

invoicedate = Convert.ToDateTime(oDocDate2)

thanks for the help

Former Member
replied

Hi Peter,

The only thing I can see wrong with your code is that you are trying to convert oDocDate2 to a datetime and not its string property (ie the value currently entered in the field).

This works for me (in C#):

SAPbouiCOM.Form sboForm = (SAPbouiCOM.Form)_sboApp.Forms.Item(ItemEvent.FormUID);
SAPbouiCOM.EditText oDocDate2 = (SAPbouiCOM.EditText)sboForm.Items.Item("10").Specific;
DateTime invoicedate = Convert.ToDateTime(oDocDate2.String);

Of course, there is no logic here to check whether a date has actually been entered in the field but that's easy to add. The value returned by the oDocDate2.String property is going to be a string in the date format you've selected for your company in Company Settings (display tab). As the .NET convert method, by default, uses your regional settings from Windows to define the date format, it would be possible to get an error trying to convert the date if SBO is not in synch with Windows (eg if your Windows is set to US format MM/DD/YYYY but the SBO company uses DD/MM/YYYY then this conversion won't work and you'll either get an incorrect date or a conversion error, depending on the date entered).

Another approach is to use the DbDataSource to read the date:

SAPbouiCOM.DBDataSource sboDS = (SAPbouiCOM.DBDataSource)sboForm.DataSources.DBDataSources.Item("OINV");
string sDate = sboDS.GetValue("DocDate", 0).TrimEnd();

This is a better approach because the datasource will always return the value in the format YYYYMMDD, regardless of the display settings in the company.

You can then do a quick conversion to turn it in to a format that .NET can convert in to a date:

sDate = sDate.Substring(0, 4) + "-" + sDate.Substring(4, 2) + "-" + sDate.Substring(6, 2);
DateTime invoicedate = Convert.ToDateTime(sDate);

Kind Regards,

Owen

0 View this answer in context
Not what you were looking for? View more on this topic or Ask a question