How to extract Date value from a DateTime field


In this post I show how to extract Date value from a DateTime field.
To do this, we need to use System.DateTime.
Please refer to the sample Job below. It will retrieve a Date value from createdDateTime field on a purchaseOrder record.



static void getDate(Args _args)
{    System.DateTime     _dateTime;    Date                         _date;    PurchTable              _purchTable;    ;
    _purchTable = PurchTable::find("PO000008");
    _dateTime = System.DateTime::Parse(dateTime2str(_purchTable.createdDateTime));
    _date = _dateTime.get_Date();
    info(date2str(_date,-1,-1,-1,-1,-1,-1));
}


Note: if you get "Request for the permission of type 'InteropPermission' failed." error, you need to use InteropPermission class:


static void getDate(Args _args)
{
    System.DateTime     _dateTime;
    Date                         _date;
    PurchTable              _purchTable;
    InteropPermission   intPerm = new InteropPermission(InteropKind::ClrInterop);
    ;
    _purchTable = PurchTable::find("PO000008");
intPerm.assert();
    _dateTime = System.DateTime::Parse(dateTime2str(_purchTable.createdDateTime));
    _date = _dateTime.get_Date();
    CodeAccessPermission::revertAssert();
    info(date2str(_date,-1,-1,-1,-1,-1,-1));
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.