Resolver One Library Documentation

API for Date

Back to documentation index page.

Classes

Class Date

class Date(object):
    def __init__(self, date, culture=None):

The Date class provides a useful way of working with dates. If you enter a string into a cell that looks like a date, then the cell Value will be an instance of Date. Dates are anything that looks like a short-form date given your regional settings. For example, "13/12/2007" would be translated as 13 December 2007 in the UK, whereas "12/13/2007" would represent the same date in the US, and "2007-12-13" would be the equivalent in Sweden.

Both day and month can be single digits as well as double digits. Years can also be in two digit form, in which case years lower than '30' will be treated as belonging to the twenty-first centry. (29 is interpreted as 2029). Years from 30 and above are treated as belonging to the twentieth century. (30 is interpreted as 1930.)

Cells containing dates are normalised (two digit years are turned into four digit years, single digit days and months are turned into double digit) and displayed right aligned in the cell.

You can also create instances of the class Date yourself in user code, and assign them to cell values.

The date argument to the constructor which you can put into your spreadsheet can be any of the following types of object:

  • A string representing the date
  • An existing Date instance
  • A .NET DateTime instance

Passing in an invalid date to the Date constructor will raise a SystemError.

Numerical Methods

The Date class has various numerical methods available on it allowing you to perform operations on dates but as dates are immutable, these return new dates rather than modifying the original.

You can add integers (positive or negative) to dates, to return the date offset by that number of days. You cannot add two dates together.

You can subtract dates from each other to return the number of days between the two dates. If the date subtracted is after the first date, then this will return a negative number of days.

Properties

DateTime

DateTime = property(get)

This 'get' only property returns the underlying .NET DateTime [1] object associated with a date.

This exposes members like Year, Month and Day; which return the appropriate integer.

[1]See http://msdn2.microsoft.com/en-us/library/system.datetime(vs.80).aspx

Methods

__eq__

def __eq__(self, other):

Returns True if compared with a date representing the same date as itself. Else it returns False.

__gt__

def __gt__(self, other):

Returns True if a date is compared with a date that is earlier than itself, else returns False.

This raises a TypeError if you compare a date with an object which is not another date.

__hash__

def __hash__(self):

Dates are immutable, so they are hashable and can be used as dictionary keys.

Dates are hashed on a tuple of their Year, Month and Day.

__ne__

def __ne__(self, other):

Returns False if compared with a date representing the same date as itself. Else it returns True.

__repr__

def __repr__(self):

This returns a string representation of the Date instance. For example:

Date('01/02/1976')

__str__

def __str__(self):

This returns a string representation, appropriate to the current locale, of the date that the instance represents. For example in the UK, the 13th of December 1976 would be:

13/12/1976

While in the US it would be:

12/13/1976

And in Sweden it would be:

1976-12-13