Manipulating date and time using Python in NiceForm and NiceLabel Portal

Time values are represented with the time class. Times have attributes for hour, minute, second, and microsecond. They can also include time zone information. A time instance only holds values of time, and not a date associated with the time.

from datetime import datetime;

time1 = datetime.time(datetime.now()); # Get current time
h = time1.hour;
m = time1.minute;
s = time1.second;
ms = time1.microsecond;
tz = time1.tzinfo;
min = time1.min;
max = time1.max;

d1.Value = "Hour: " + str(h);
d2.Value = "Minute: " + str(m);
d3.Value = "Second: " + str(s);
d4.Value = "Microsecond: " + str(ms);
d5.Value = "Time zone: " + str(tz);
d6.Value = "Earliest: " + str(min);
d7.Value = "Latest: " + str(max);

Calendar date values are represented with the date class. Instances have attributes for year, month, and day. It is easy to create a date representing current date using the today() class method. As with time, the range of date values supported can be determined using the min and max attributes.

from datetime import date;

date1 = date.today(); # Get current date
year = date1.year;
month = date1.month;
day = date1.day;
ordinal = date1.toordinal();
min = date1.min;
max = date1.max;

d1.Value = "Day: " + str(day);
d2.Value = "Month: " + str(month);
d3.Value = "Year: " + str(year);
d4.Value = "Ordinal: " + str(ordinal);
d5.Value = "Earliest: " + str(min);
d6.Value = "Latest: " + str(max);

How can I add or subtract from a date?

To add or subtract from a date use the datetime.timedelta object.

from datetime import datetime, timedelta, time;

time1 = datetime.now();
diff = timedelta(weeks=52);  # 1 year
time2 = time1 + diff;
diff = timedelta(days=730);  # 2 years
time3 = time1 + diff;
diff = timedelta(hours=240);  # 10 days
time4 = time1 + diff;
diff = timedelta(weeks=40, days=84, hours=23, minutes=50, seconds=600);  # Adds up to 365 days
time5 = time1 - diff;

d1.Value = "Current date and time: " + str(time1);
d2.Value = "Added 52 weeks (1 year): " + str(time2);
d3.Value = "Added 730 days (2 years): " + str(time3);
d4.Value = "Added 10 days: " + str(time4);
d5.Value = "Subtracted 365 days: " + str(time5);

How can I calculate difference of two dates?

When two dates are subtracted they produce timedeltas.

from datetime import date;

date1 = date.today();
date2 = date(1973, 7, 6);
diff = date1 - date2;

d1.Value = "Current date: " + str(date1);
d2.Value = "My birthday: " + str(date2);
d3.Value = "I am : " + str(diff.days) + " days old.";

Can I compare date or time values?

Both time and date values can be compared using standard operators to determine which one is earlier or later.

from datetime import date, time;

date1 = date.today();
date2 = date(1973, 7, 6);
time1 = time(12, 0, 0);
time2 = time(13, 5, 0);

d1.Value = "Date 1: " + str(date1);
d2.Value = "Date 2: " + str(date2);
d3.Value = "Date 1 < Date 2: " + str(date1 < date2);
d4.Value = "Time 1: " + str(time1);
d5.Value = "Time 2: " + str(time2);

d6.Value = "Time 1 > Time 2: " + str(time1 < time2);

How can I replace just part of date?

from datetime import datetime;

date1 = datetime.now();            # Use current date and time
date2 = date1.replace(year=2012);  # Change year to 2012
date3 = date1.replace(month=12);   # Change month to December
date4 = date1.replace(day=31);     # Change day to 31
date5 = date1.replace(day=31, month=12, year=2012); # Change day, month and year all at once.

d1.Value = str(date1);
d2.Value = str(date2);
d3.Value = str(date3);
d4.Value = str(date4);
d5.Value = str(date5);

Formatting time and date

Default string representation of a datetime object uses the ISO 8601 format (YYYY-MM-DDHH:MM:SS.mmmmmm). Other formats can be generated using strftime() method. The following directives can be embedded in the format string:

Directive Meaning
%a Locale’s abbreviated weekday name.
%A Locale’s full weekday name.
%b Locale’s abbreviated month name.
%B Locale’s full month name.
%c Locale’s appropriate date and time representation.
%d Day of the month as a decimal number [01,31].
%H Hour (24-hour clock) as a decimal number [00,23].
%I Hour (12-hour clock) as a decimal number [01,12].
%j Day of the year as a decimal number [001,366].
%m Month as a decimal number [01,12].
%M Minute as a decimal number [00,59].
%p Locale’s equivalent of either AM or PM.
%S Second as a decimal number [00,61].
%U Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0.
%w Weekday as a decimal number [0(Sunday),6].
%W Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0.
%x Locale’s appropriate date representation.
%X Locale’s appropriate time representation.
%y Year without century as a decimal number [00,99].
%Y Year with century as a decimal number.
%Z Time zone name (no characters if no time zone exists).
%% A literal '%' character.

Here are some examples of using different directives:

from datetime import date;

format1 = "%A,  %B %d, %Y";
format2 = "Today is day %j of the year.";
format3 = "Short month name: %b; Short weekday name: %a";

date1 = date.today();
d1.Value = date1.strftime(format1);
d2.Value = date1.strftime(format2);
d3.Value = date1.strftime(format3);

How can I get a week number from a date?

from datetime import date;

format = "Today is day %w of the %W week of the year.";

date1 = date.today();
d1.Value = date1.strftime(format);

All code samples above should work in NiceForm and NiceLabel Portal. You can find all samples in sample form file (PyDateTime.xff).

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