chrono.Date - Main class for date handling

class chrono.Date(date=None, parser=None, calendar=None, **kwargs)

A class for date handling. For general usage, see the Usage section.

Valid values for date can be:

  • string: parses date from a string using the given parser (by default chrono.parser.CommonParser)
  • True: sets the date to the current date
  • integer: assumes input is a UNIX timestamp, sets date accordingly
  • chrono.Date: sets date from another Date object
  • datetime.date: sets date from a datetime.date object
  • time.struct_time: sets date from a time.struct_time object
  • None: creates a date with empty attributes
  • False: creates a date with empty attributes

The class can also be initialized using the keyword arguments year, month, and day:

Date(year=2000, month=10, day=16)

If both date and keywords are specified, date takes precedence.

parser determines which parser to use for parsing dates from strings. By default chrono.parser.CommonParser is used, which supports the most common date and time formats. See chrono.parser for a list of available parsers.

calendar determines which calendar to use for calendar operations. By default chrono.calendar.ISOCalendar is used, see chrono.calendar for a list of available calendars.

assert_set()

Makes sure the object has a full date set, ie the attributes chrono.Date.year, chrono.Date.month, and chrono.Date.day are not None.

Raises chrono.error.NoDateTimeError on missing attributes.

clear()
Clears the date, by setting chrono.Date.year, chrono.Date.month and chrono.Date.day to None.
format(template)

Formats the date using template, replacing variables as supported by chrono.formatter.Formatter. This value is dependent on the calendar set in chrono.Date.calendar, by default chrono.calendar.ISOCalendar.

Raises chrono.error.NoDateTimeError on missing date data.

get()

Returns the date as a tuple of year, month, and day.

Raises chrono.error.NoDateTimeError on missing date data.

get_datetime()

Returns a datetime.date instance based on the date.

Raises chrono.error.NoDateTimeError on missing date data.

get_string()

Returns a string representation (yyyy-mm-dd) of the date.

Raises chrono.error.NoDateTimeError on missing date data.

get_struct_time()

Returns a time.struct_time representation of the date (expected as input to many Python functions).

Raises chrono.error.NoDateTimeError on missing date data.

get_unix()

Returns a UNIX timestamp representation of the date.

Raises chrono.error.NoDateTimeError on missing date data.

is_set()
Returns True if a date is set, ie if the attributes chrono.Date.year, chrono.Date.month and chrono.Date.day are not None, otherwise returns False.
leapyear()

Returns True if the date is in a leap year, otherwise False.

Raises chrono.error.NoDateTimeError on missing date data.

monthdays()

Returns the number of days in the set month.

Raises chrono.error.NoDateTimeError on missing date data.

ordinal()

Returns the ordinal day (day number in the year) of the set date.

Raises chrono.error.NoDateTimeError on missing date data.

set(year, month, day)

Sets the date.

Raises chrono.error.YearError, chrono.error.MonthError, or chrono.error.DayError for invalid values.

set_datetime(datetime)
Sets the date from a datetime.date object.
set_now()
Sets the date to the current date.
set_string(string)

Sets the date from a string, parsed with the parser set in chrono.Date.parser, by default chrono.parser.CommonParser.

Raises chrono.error.ParseError for invalid input format, TypeError for invalid input type, and an appropriate chrono.error.DateError subclass for invalid date values.

set_struct_time(struct_time)
Sets the date from a time.struct_time (as returned by various Python functions).
set_unix(timestamp)
Sets the date from an integer UNIX timestamp.
week()

Returns the week of the set date as a tuple with year and week number. This value is dependent on the calendar set in chrono.Date.calendar, by default chrono.calendar.ISOCalendar.

Raises chrono.error.NoDateTimeError on missing date data.

weekdate()

Returns the week date of the set date as a tuple with year, week, and weekday. This value is dependent on the calendar set in chrono.Date.calendar, by default chrono.calendar.ISOCalendar.

Raises chrono.error.NoDateTimeError on missing date data.

weekday()

Returns the week day of the set date, or None if no date is set. This value is dependent on the calendar set in chrono.Date.calendar, by default chrono.calendar.ISOCalendar.

Raises chrono.error.NoDateTimeError on missing date data.

weeks()

Returns the number of weeks in the set year. This value is dependent on the calendar set in chrono.Date.calendar, by default chrono.calendar.ISOCalendar.

Raises chrono.error.NoDateTimeError on missing date data.

yeardays()

Returns the number of days in the year.

Raises chrono.error.NoDateTimeError on missing date data.

calendar
Calendar to use for calendar operations. Defaults to chrono.calendar.ISOCalendar. See chrono.calendar for available calendars.
day
Day number, range 1-31 depending on chrono.Date.month and chrono.Date.year.
month
Month number, range 1-12.
parser
Parser to use for parsing dates and times from strings. See chrono.parser for available parsers.
year
Year number, range 1-9999.

Previous topic

Usage

Next topic

chrono.DateTime - Main class for datetime handling