web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

use str2Date function with date imported from excel

(0) ShareShare
ReportReport
Posted on by

I try to use the following code to get date from excel sheet but the date value after using str2date is empty although sEmpDate value is "04/04/1993"

 

date  EmpDate;

str   sEmpDate; 

 

 

sEmpDate=cells.item(row, 3).value().bStr();           

EmpDate=str2Date(sEmpDate,123);

info(strFmt("%1",sEmpDate));       

info(strFmt("%1",EmpDate));

*This post is locked for comments

I have the same question (0)
  • Bilal Issa Profile Picture
    4,370 on at
    RE: use str2Date function with date imported from excel

    Hi ,

    please try to replace '/' with '-' before converting it to date.

    Regards,

    Bilal

  • Martin Dráb Profile Picture
    236,394 Most Valuable Professional on at
    RE: use str2Date function with date imported from excel

    I can't confirm your findings. This code works:

    date empDate = str2Date('04/04/1993', 123);
    info(strFmt("%1", empDate));

    Please try it, If it works for you too, you have a problem somewhere else.

  • Piyush Adhikari Profile Picture
    2,546 on at
    RE: use str2Date function with date imported from excel

    strange ........should work.....try to debug and exact value in variable sEmpDate..

    Btw u can also use cells.item(row, 3).value().date();

  • Suggested answer
    Faisal Fareed Profile Picture
    10,796 User Group Leader on at
    RE: use str2Date function with date imported from excel

    Hi Maram,

    I don't see anything wrong with this code and it works for me. You need to debug your code and see what value is setting here sEmpDate=cells.item(row, 3).value().bStr(); from excel file. You might be reading from wrong column on excel sheet and there is something else wrong in reading from excel file. Could you please share your complete code it will be easy for us to guide you.

  • maram fraij Profile Picture
    on at
    RE: use str2Date function with date imported from excel

    Please, find below the code, and the attachment in the next post has a screenshot for the results. If the date string is retrieved from excel ,str2date does not work EmpDate is empty. On the other hand, if the string is provided directly, str2date works properly  and EmpDate2 has the correct value.

    static void test(Args _args)

    {

       SysExcelApplication application;

       SysExcelWorkbooks workbooks;

       SysExcelWorkbook workbook;

       SysExcelWorksheets worksheets;

       SysExcelWorksheet worksheet;

       SysExcelCells cells;

       COMVariantType type;

       int row=1;

       str sheetName,Empname;

       date  EmpDate,EmpDate2;

       str   sEmpDate,sEmpDate2;

       ;

       #define.filename(@'C:\temp\EmpVacBalance.xlsx')

       sheetName="Sheet1";

       application = SysExcelApplication::construct();

       workbooks = application.workbooks();

       try

       {

           workbooks.open(#filename);

       }

       catch (Exception::Error)

       {

           throw error("File not found");

       }

       workbook = workbooks.item(1);

       worksheets = workbook.worksheets();

       worksheet = worksheets.itemFromName(sheetName);

       cells = worksheet.cells();

       //Iterate through cells and get the values

       do

       {

           try

           {

               //Incrementing the row line to next Row

               row++;

               Empname=cells.item(row, 2).value().bStr();

               info(Empname);

               sEmpDate=cells.item(row, 3).value().bStr();  

               EmpDate=str2Date(sEmpDate,123);

               info(strFmt("%1",sEmpDate));

               info(strFmt("%1",EmpDate));

               sEmpDate2="04/04/1993";

               EmpDate2=str2Date(sEmpDate2,123);

               info(strFmt("%1",sEmpDate2));

               info(strFmt("%1",EmpDate2));

           }

           catch

           {

              error("error in this row");

           }

           // Loads the next row into the variant type and validating that its is empty or not

           type = cells.item(row+1, 1).value().variantType();

       }

       while (type != COMVariantType::VT_EMPTY);

       // quits the application

       application.quit();

    }

  • maram fraij Profile Picture
    on at
    RE: use str2Date function with date imported from excel

  • maram fraij Profile Picture
    on at
    RE: use str2Date function with date imported from excel

  • Verified answer
    Martin Dráb Profile Picture
    236,394 Most Valuable Professional on at
    RE: use str2Date function with date imported from excel

    If the strings were the same, the behavior would be the same too. It means that the value got from Excel isn't what you think it is - check for white spaces, non-printable characters and such things.

  • maram fraij Profile Picture
    on at
    RE: use str2Date function with date imported from excel

    Yes Martin, it works now.

    Thank you

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Community Member Profile Picture

Community Member 4

#1
Martin Tocauer Profile Picture

Martin Tocauer 4

#3
Nayyar Siddiqi Profile Picture

Nayyar Siddiqi 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans