Showing posts with label Date. Show all posts
Showing posts with label Date. Show all posts

Friday, March 18, 2011

How to calculate date displacements, working with days, months and years.

When working with date displacements in Dynamics Ax, you can use simple mathematics.
For example, go 3 days back can be written out like:

newDate = today() - 3;

It cannot get any easier.
But this is for days.  May be we want to go back in time or into the future, working with full months or years.  And then the number of days in a month comes into picture.  Mmmm

We can use the DateTimeUtil class from Dynamics Ax to achieve this.  Like we saw in the previous post, this class offers all sorts of calculations for the date.  Use AddDays, AddMonths, AddYears to calculate the date after the offset, working with specific period units.  Use negative arguments to go back in time.
Example:

DateTimeUtil::addMonths(TransDateTime,3);

I've included a short job, that is sort of an extension to the DateTimeUtil class.
It can calculate date displacements for 3 different period units.

The code:

static void DateDiffA(Args _args)
{
   date TransDate;

   TransDate Calcdate(date       startDate,
                      Periods    periodQty,
                      PeriodUnit periodUnit)
   {
      TransDateTime TransDateTime=DateTimeUtil::newDateTime(startDate,0);

      switch (periodUnit)
      {
         case PeriodUnit::Day:
            return any2date(DateTimeUtil::addDays(TransDateTime,periodQty));

         case PeriodUnit::Month:
            return any2date(DateTimeUtil::addMonths(TransDateTime,periodQty));

         case PeriodUnit::Year:
            return any2date(DateTimeUtil::addYears(TransDateTime,periodQty));
      }

      throw error(Error::wrongUseOfFunction(funcname()));
   }
   ;

   TransDate=CalcDate(today(),3,PeriodUnit::Month);

   info(strfmt('The calculated date is %1',date2str(TransDate,123,2,2,2,2,4)));
} 

You can use this code as inspiration to create a new method for the Global class, so you can use it all over in Dynamics Ax.

Friday, February 25, 2011

How to get the first and the last day of a month

When working with dates for a range in a SQL select statement, you may be in need for the first and the last day of a certain month.  What at first seems a bit difficult to calculate (not all the months have the same number of days for example), is pretty easy to accomplish with the right tools.

 
The first day of a month

 
You can fool around with things like mkdate, so something like this:

 
static void FirstOfMonth(Args _args)
{ 
   TransDate TransDate=today();
   TransDate FirstOfMth;
   ;

   FirstOfMth=mkdate(1,mthofyr(TransDate),year(TransDate));

   info(date2str(FirstOfMth,123,2,2,2,2,4));
} 
 
But there is a readily available function in the Global class, albeit somewhat hidden: DateStartMth.
 
So your code could read like this:
 
static void FirstOfMonth(Args _args) 
{
   TransDate TransDate=today();
   TransDate FirstOfMth;
   ;
   FirstOfMth=DateStartMth(TransDate);

   info(date2str(FirstOfMth,123,2,2,2,2,4));
} 

 
The last day of a month

 
Just like with the first day of a month, we have a function that will do the job for us: endmth
This function calculates the last date in the month of the date specified.

static void LastOfMonth(Args _args)
{
   TransDate TransDate=today();
   TransDate LastOfMth;
   ;

   LastOfMth=endmth(TransDate);

   info(date2str(LastOfMth,123,2,2,2,2,4));
}
 
You may want to check out following functions as well, with some other date functionality:

Tuesday, September 15, 2009

How to populate a date variable

Some quick tips on how to populate a date variable.

Example 1: The use of function mkdate

TransDate myDate=mkdate(15,9,2009);

This function creates a date based on three integers, which indicate day, month and year. So mkdate(day,month,year).

As an alternative, you can use this:

Example 2: The use of function str2date

TransDate myDate=str2date('15/09/2009',123);

The function str2date converts a text string to a date value. The second argument gives the used formatting of the date string. (Where 1 stands for day, 2 for month and 3 for year.)

There is a similar function to create a DateTime value, namely str2datetime.
Use:

TransDateTime myDateTime=str2datetime( "15/09/2009 20:39:00" ,123 );

So, plenty of choice, whichever suits you.