How can I calculate week number and year in Python in NiceForm?

Sometimes you need to have a year and/or week number on a label or in a form. The following script calculates both.

import sys
if sys.platform == 'silverlight':
    import System
    from System.Globalization import *
    culture = System.Globalization.CultureInfo.CurrentCulture;
    dt = System.DateTime.Now
    dfi = System.Globalization.DateTimeFormatInfo.CurrentInfo;      
    cal = dfi.Calendar;
    WeekNumber.Value = cal.GetWeekOfYear(dt, dfi.CalendarWeekRule, dfi.FirstDayOfWeek).ToString()
    fullYear = dt.Year.ToString()
    Year.Value = fullYear[2:4]
else:
    from datetime import datetime
    dt = datetime.now()
    WeekNumber.Value = dt.isocalendar()[1]
    Year.Value = dt.isocalendar()[0]

The Python script should work in both NiceForm on the desktop and in NiceLabel Portal and NiceLabel Breeze in the web browser.

When designing form in NiceForm make sure you have changed scripting language to Python in the “Form Properties” dialog box (see screenshot below). You also need to have two variables defined in the form: WeekNumber and Year. You can put the code in the “On Click” event of the button by using “Execute script” action.

Form Properties - Scripting Language 

Posted in NiceForm, NiceLabel Breeze, NiceLabel Portal Tagged with: , , , ,