Thursday, July 14, 2011

Get day, month and year from date in axapta x++

Hi, everybody!


In Dynamics AX, sometimes we need to get the day, month or year from a date and we don't know how to do this... but we can do this easily...


//Get day from date
dayOfMth(systemdateget())

//Get month from date
mthOfYr(systemdateget())

//Get year from date
year(systemdateget())


Good DaXing!


Please vote +1 if you like this post.

11 comments:

  1. What if we want to get the date part from a field in a table for use in a query or view?

    ReplyDelete
  2. Hi Stephen,

    If the field is a date type, you can use this functions.

    Good DaXing!

    ReplyDelete
  3. How can I use this in a method that is in view to return a calculated column?

    ReplyDelete
  4. Hi Stephen,

    You can use this if the method returns a date value.

    Good DaXing!

    ReplyDelete
  5. This doesn't work when I try to get it from the value that is returned from the DictView.computedColumnString( I don't get good values. I get 1900 for year and 1 for the month no matter what the date is. I've tried converting to string and back to date and still have the problem. If I leave the date in it's delivered from (from DictView.computedColumnString() then it reads as Oct, 18, 2012 for example.

    ReplyDelete
  6. Hi Stephen,

    I don't know why it happens, but these functions work with a date value.

    Good DaXing!

    ReplyDelete
  7. I got it working. You can't use the date functions against a date that is returned by the computedColumnString method. To get a date part for a computed column you have to query the table and use SQL date functions. My working class is attached.

    private static server str compFiscalYear()
    {
    #define.ViewName(LedgerJournalDimension)
    #define.DataSourceName("LedgerJournalTrans")
    #define.FieldDisplayValue("RecID")

    str sWhereClause1;
    DictView dictView;
    SysDictTable dimJournalTable;

    // Construct a DictView object for the present view.
    dictView = new DictView(tableNum(#ViewName));
    //Construct a table object for the account table
    dimJournalTable = new SysDictTable(tableNum(LedgerJournalTrans));


    // Create the where clause
    sWhereClause1 = DictView.computedColumnString(
    'LedgerJournalTrans',
    fieldStr(LedgerJournalTrans, RecId),
    FieldNameGenerationMode::WhereClause);

    return strFmt('select year(TransDate) from %1 where %2 = %3',
    dimJournalTable.name(DbBackend::Sql),
    dimJournalTable.fieldName(fieldNum(LedgerJournalTrans, RecId), DbBackend::Sql),
    sWhereClause1);

    }

    ReplyDelete
  8. Hi Stephen,

    Nice work, I think you've found a good solution for a complicated problem.

    Thanks in advance for your feedback and I hope to see you back on the blog.

    Good DaXing!

    ReplyDelete
  9. Hi to all,
    How can I filter date field in form to show from current year and skip date from previous years?
    In AX 2009

    ReplyDelete
  10. Hi Miljan,

    Do you want to do it by code?

    Good DaXing!

    ReplyDelete
  11. Hi Jordi,
    I have an old form that uses table in which field Date has records from 01/01/2013 and I need to start shoving records from current year(so now it should be 01/01/2014 and next year 01/01/2015) and i have tried
    this.query().dataSourceNo(1).addRange(fieldnum(Table, Date)).value("> DateStartYr(today())");
    and it shows records from today(22/03/2014) not from first day of year... Because of frustration i have tried value("> 31/12/2013"); and it works perfectly but it is not a permanent solution so please can anyone help me?

    ReplyDelete