piku
piku

Reputation: 613

WeekOfYear in X++ using .net lib

 static void Job5(Args _args)
    {
     int i;
     System.DateTime netDttm;
     System.Int32 intnet;
    ;
     netDttm = new System.DateTime(2011,03,20 ,13,44,55);
     intnet = System.Globalization.CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(netDttm, Globalization.CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Sunday);

     i = intnet;

     info(int2str(i));
   }

I tried the in vb.net it works fine but doing the same in x++(using .net lib) it shows syntax error..All I am trying is to get the week no. from a supplied date. Any insight would be appreciated.

P.S. I found another solution to this which is I created a dll file in VS .net and added this to Reference node(AOT)of AX. It has shorten the code in AX static void Job5(Args _args) { weekofyear.wof asd; ; asd = new weekofyear.Wof(); print asd.weekofyr(today()); pause; }

Upvotes: 2

Views: 1790

Answers (3)

Skaue
Skaue

Reputation: 783

Just make sure you load the correct CultureInfo if you expect this code to support word wide locations. Loading the current CultureInfo will load the servers preferred culture. If the user is en-gb and the server is en-us, you first day of week will be incorrect.

To load a specific cultureinfo you can simply do this:

System.Globalization.CultureInfo arCul = new System.Globalization.CultureInfo("en-US");

In the example chosen as answer, the code loads cultureinfo, but the cultureinfo is not used as parameter to the GetWeekOfYear method, which doesnt really make any sense. Instead you could send in the settings from the cultureinfo.

Upvotes: 1

Gareth McCaughan
Gareth McCaughan

Reputation: 19981

[Note to any future readers: The following described an error in the original code Indranil posted; it does not apply to the code currently in the question, because Indranil fixed this error. The other error was dealt with in another answer from someone else :-).]

You shouldn't be passing a string as the first argument to GetWeekOfYear; it wants a System.DateTime (http://msdn.microsoft.com/en-us/library/system.globalization.calendar.getweekofyear.aspx). (At least, that's true in ordinary .NET; I don't know whether Dynamics AX does some other magical thing. I doubt it does.)

(But if and when you do want a date in the form of a string, those backslashes \ should be forward slashes /.)

Upvotes: 2

David Lawson
David Lawson

Reputation: 796

try this

int i;
     System.DateTime netDttm;
     System.Int32 intnet;
     System.Globalization.CultureInfo cultureInfo;
     System.Globalization.Calendar    calendar;
     System.Globalization.CalendarWeekRule calWeekRule
    ;
     netDttm = new System.DateTime(2011,03,20 ,13,44,55);
     cultureInfo = System.Globalization.CultureInfo::get_CurrentCulture();
     calendar  = cultureInfo.get_Calendar();
     intnet = calendar.GetWeekOfYear(netDttm, System.Globalization.CalendarWeekRule::FirstFourDayWeek, System.DayOfWeek::Sunday);

     i = intnet;

 info(int2str(i));

Upvotes: 4

Related Questions