Dates

Chapter Updated 07/08/26




Class xbDate - Date Class

The xbDate class is used for storing and manipulating date values.

The date is stored internally as ab eight byte text field in CCYYMMDD format.

Methods for setting the date format at the system level:

  • xbDate::SetDefaultDateFormat()
  • xbDate::GetDefaultDateFormat()


  • Methods for setting the date format at the instance level (over ride default setting):

  • xbDate::SetDateFormat()
  • xbDate::GetDateFormat()


  • Methods for retrieving date values:

  • xbDate::Str() - Returns date string in CCYYMMDD format.
  • xbDate::Strf() - Returns date string in current specified format.

  • xbDate::FormatDate() - Depreciated method. Will be removed in future releases.
  • xbDate::GetFormattedDate() - Return a date in a given format.


    Format specifiers for above six functions.

    Format specifiers are replaced by the function to the corresponding values to represent the xbDate.
    All specifiers begin with a percentage (%) sign. For example: xbString sMyFormatSpecifier = "%Y-%m-%d";

    SpecifierReplaced byExample
    %aAbbreviated weekday nameThu
    %AFull weekday nameThursday
    %bAbbreviated month nameAug
    %BFull month nameAugust
    %cDate and time representationThu Aug 23 14:55:02 2001
    %CYear divided by 100 and truncated to integer (00-99)20
    %dDay of the month, zero-padded (01-31)23
    %DShort MM/DD/YY date, equivalent to %m/%d/%y08/23/01
    %eDay of the month, space-padded ( 1-31)23
    %FShort YYYY-MM-DD date, equivalent to %Y-%m-%d2001-08-23
    %gWeek-based year, last two digits (00-99)01
    %GWeek-based year2001
    %hAbbreviated month name (same as %b)Aug
    %jDay of the year (001-366)235
    %mMonth as a decimal number (01-12)08
    %nNew-line character
    %tHorizontal-tab character
    %uISO 8601 weekday as number with Monday as 1 (1-7)4
    %UWeek number with the first Sunday as the first day of week one (00-53)33
    %VISO 8601 week number (01-53) 34
    %wWeekday as a decimal number with Sunday as 0 (0-6) 4
    %WWeek number with the first Monday as the first day of week one (00-53)34
    %xDate representation08/23/01
    %yYear, last two digits (00-99)01
    %YYear2001
    %%A % sign%


    Class xbDate methods

    MethodDescription
    xbDate()
    xbDate( const char * sDate8 )
    xbDate( const xbDate &dtDate )
    xbDate( const xbString &sDate8 )
    xbDate( xbInt16 iMonth, xbInt16 iDay, xbInt16 iYear4 )
    xbDate( time_t tInDt )
    Various xbDate constructors.
    xbDate( xbUInt32 iOpt ) Initialization xbDate constructor or Julian date.
    -1 Null Date
    0 Sysdate
    Julian Date
    ~xbDate()Destructor
    xbInt16 CalcRollingCenturyForYear( xbInt16 year ) constCalculate century for a give 2 digit year. Uses 80/20 method.
    xbInt16 CenturyOf() constReturn century of the date.
    xbInt16 CharDayOf( xbString &sOutCharDay )Retrieve the character day.
    xbInt16 CharMonthOf( xbString &sOutCharMonth )Retrive the character month.
    xbInt16 CTOD( const xbString &sCtodInDate )Sets the date from input date in format MM/DD/YY
    xbBool DateIsValid ( const xbString &sDate8 ) const;Check if a given date is valid.
    xbInt16 DayOf( xbInt16 iFormat ) constReturn Day of week, month or year depending on the option.
    void Dump( const char * sTitle )Dump class internals.
    Available if XB_DEBUG_SUPPORT option compiled into library.
    void DumpDateTables()Dump internal date structures.
    Available if XB_DEBUG_SUPPORT option compiled into library.
    xbInt16 FormatDate( const xbString &sFmtIn, xbString &sFmtOut )

    Depreciated method.
    This is the original xbase date format routine and uses a different set of format specifiers.
    Xbase64 users are advised to migrate away from using this method and use the newer versions.
    Support for this method will be dropped in future versions.
    Create a formatted date string based on input format.
  • Valid Date Format Specifiers for FormatDate():

    1) YYDDD - A julian date format
    2) YY or YYYY will print a 2 or 4 digit year
    3) M,MM,MMM or MMMM
  • M - one digit month if no leading zero
  • MM - two digit month, contains leading zero
  • MMM - Jan through Dec
  • MMMM - January through December
    4) D,DD,DDD or DDDD
  • D - one digit dayif no leading zero
  • DD - two digit day, contains leading zero
  • DDD - Sun through Sat (or julian if YYDDD)
  • DDDD - Sunday through Saturday

    Format Examples:
  • MM/DD/YY
  • YYYY-MM-DD
  • DDDDDDDDDDD MMMMMMMMMMM DD,YYYY

    The default format is MM/DD/YY
  • const xbString &GetDateFormat() constReturns the date format for a given date.
    Returns the date format if set, else returns the system date format.
    const xbString &GetDefaultDateFormat() constRethe default date format.
    xbBool IsLeapYear( xbInt16 iYear ) const
    xbBool IsLeapYear() const
    Returns true if leap year, otherwise returns false.
    xbBool IsNull() constReturns xbTrue is the date is null, else returns false.
    xbInt32 JulianDays() constReturn the Julian days value.
    xbInt16 JulToDate8( xbInt32 lJulDate )Convert a Julian date to xbDate value.
    xbInt16 LastDayOfMonth()Calculate the last day of a month for the date object.
    xbInt16 MonthOf() constReturn the numeric month of the date.
    xbBool operator!=( const xbDate & ) constCompare for not equal dates.
    const char * operator+( xbInt32 i )Add i days to a date.
    void operator++( xbInt32 i )Increment a date.
    void operator+=( xbInt32 i )Increment a date.
    xbInt32 operator-( const xbDate & ) constDetermine the number of days between two dates.
    const char * operator-( xbInt32 i )Subtract i days from a date.
    void operator--( xbInt32 i )Decrement a date.
    void operator-=( xbInt32 i )Decrement a date.
    xbBool operator< ( const xbDate & ) constCompare for less than date.
    xbBool operator<=( const xbDate & ) constCompare for less than or euql to date.
    void operator=( const xbDate &d )Set a date equal to another date.
    xbBool operator==( const xbDate & ) constCompare for equal dates.
    xbBool operator> ( const xbDate & ) constCompare for greater than date.
    xbBool operator>=( const xbDate & ) constCompare for greater than or equal to date.
    xbInt16 Set( const xbString &Date8 )
    xbInt16 Set( xbInt16 iMonth, xbInt16 iDaty, xbInt16 iYear)
    Set the date to valid input date.
    void SetDateFormat( const char *sFmtIn )
    void SetDateFormat( xbString &sFmtIn )
    Set date format for a given date.
    void SetDefaultDateFormat( const xbString &sFmtIn )Set default date format.
    void SetNull()Set the date to null.
    const char *Str() constReturn a pointer the date value Returns CCYYMMDD value.
    const char *Strf() constReturn a pointer the formatted date value as determined by the specified date format.
    void Sysdate()Set the date to the system date.
    xbInt16 YearOf() constReturn the numeric year of the date.





    Example program using xbDate class

    /* xb_ex_date.cpp XBase64 Software Library Copyright (c) 1997,2003,2014,2021,2022,2023,2024 Gary A Kunkel The xb64 software library is covered under the terms of the GPL Version 3, 2007 license. Email Contact: XDB-devel@lists.sourceforge.net XDB-users@lists.sourceforge.net This program demonstrates using the xbDate class */ #include "xbase.h" using namespace xb; int main() { xbXBase x; /* initial date static variables */ xbString StringDate( "19601007" ); /* oct 7 1960 */ char CharDate[9] = "19611109"; /* nov 9 1961 */ xbDate d1; d1.Sysdate(); /* set to SysDate */ xbDate d2( StringDate ); /* from string data */ xbDate d3( CharDate ); /* from char data */ xbDate d4; /* another date class */ xbDate d5( d2 ); /* set new date d5 equal to date d2 */ xbString s; std::cout << "This program demonstrates usage of the xbDate class" << std::endl; std::cout << "Date 1 (Sysdate) is " << d1.Str() << std::endl; std::cout << "Date 2 (StringDate) is " << d2.Str() << std::endl; std::cout << "Date 3 (CharDate) is " << d3.Str() << std::endl; std::cout << "This year is " << d1.YearOf() << std::endl; std::cout << "Year of xbString Date is " << d2.YearOf() << std::endl; std::cout << "Year of char Date is " << d3.YearOf() << std::endl; std::cout << "This Month is " << d1.MonthOf() << std::endl; std::cout << "Month of xbString Date is " << d2.MonthOf() << std::endl; std::cout << "Month of char Date is " << d3.MonthOf() << std::endl; std::cout << "Today is day " << d1.DayOf( XB_FMT_WEEK ) << " of the week" << std::endl; std::cout << "StringDate is day " << d2.DayOf( XB_FMT_MONTH ) << " of the month" << std::endl; std::cout << "CharDate is day " << d3.DayOf( XB_FMT_YEAR ) << " of the year" << std::endl; if( d1.IsLeapYear()) std::cout << "This is a leap year" << std::endl; else std::cout << "This is not a leap year." << std::endl; if( d2.IsLeapYear()) std::cout << "StringDate is a leap year" << std::endl; else std::cout << "StringDate is not a leap year." << std::endl; if( d3.IsLeapYear()) std::cout << "CharDate is a leap year" << std::endl; else std::cout << "CharDate is not a leap year." << std::endl; d1.Sysdate(); std::cout << "Today is " << d1.Str() << std::endl; if( d1.DateIsValid( "19951301" )) std::cout << "19951301 is a valid date" << std::endl; else std::cout << "19951301 is not a valid date" << std::endl; if( d1.DateIsValid( "19920229" )) std::cout << "19920229 is a valid date" << std::endl; else std::cout << "19920229 is not a valid date" << std::endl; if( d2.DateIsValid( StringDate )) std::cout << StringDate.Str() << " is a valid date" << std::endl; else std::cout << StringDate.Str() << " is not a valid date" << std::endl; std::cout << "Today's Julian date " << d1.JulianDays() << std::endl; std::cout << "Julian date of Jan 01, 1970 " << d2.JulianDays() << std::endl; std::cout << "StringDate Julian date " << d2.JulianDays() << std::endl; std::cout << "There are " << (d1.JulianDays() - d2.JulianDays()) << " days between " << d1.Str() << " and " << d2.Str() << std::endl; std::cout << "Todays Julian date (Number of days since Jan 1 0100):" << d1.JulianDays() << std::endl; d4 = d1; // set d4 class = to sysdate std::cout << "Object d4 initialized to " << d4.Str() << std::endl; d1.CharDayOf( s ); std::cout << "Today is " << s.Str() << std::endl; d2.CharDayOf( s ); std::cout << "StringDate day is " << s.Str() << std::endl; d1.CharMonthOf( s ); std::cout << "This month is " << s.Str() << std::endl; d2.CharMonthOf( s ); std::cout << "StringDate month is " << s.Str() << std::endl; /* various format routines using different formats, strings and chars */ xbString sOutDate; d1.FormatDate( "YYDDD", sOutDate ); std::cout << "Format (YYDDD) = " << sOutDate.Str() << std::endl; d1.FormatDate( "MM/DD/YY", sOutDate ); std::cout << "Format (MM/DD/YY) = " << sOutDate.Str() << std::endl; d1.FormatDate( "YY-MM-DD", sOutDate ); std::cout << "Format (YY-MM-DD) = " << sOutDate.Str() << std::endl; d1.FormatDate( "DDDDDDDDD, MMMMMMMMMM DD YYYY", sOutDate ); std::cout << "Format (DDDDDDDDD, MMMMMMMMMM DD YYYY) = " << sOutDate.Str() << std::endl; d1.LastDayOfMonth(); std::cout << "Last day this month " << d1.Str() << std::endl; d2.LastDayOfMonth(); std::cout << "Last day of month for StringDate is " << d2.Str() << std::endl; std::cout << "Operators test..." << std::endl; if( d1 == d2 ) std::cout << d1.Str() << " is equal to " << d2.Str() << std::endl; else std::cout << d1.Str() << " is not equal to " << d2.Str() << std::endl; if( d1 != d3 ) std::cout << d1.Str() << " is not equal to " << d3.Str() << std::endl; else std::cout << d1.Str() << " is equal to " << d3.Str() << std::endl; if( d1 < d2 ) std::cout << d1.Str() << " is less than " << d2.Str() << std::endl; else std::cout << d1.Str() << " is not less than " << d2.Str() << std::endl; if( d1 > d2 ) std::cout << d1.Str() << " is greater than " << d2.Str() << std::endl; else std::cout << d1.Str() << " is not greater than " << d2.Str() << std::endl; if( d1 <= d2 ) std::cout << d1.Str() << " is less than or equal to " << d2.Str() << std::endl; else std::cout << d1.Str() << " is not less than or equal to " << d2.Str() << std::endl; if( d1 >= d2 ) std::cout << d1.Str() << " is greater than or equal to " << d2.Str() << std::endl; else std::cout << d1.Str() << " is not greater than or equal to " << d2.Str() << std::endl; d1.Sysdate(); d1++; std::cout << "Tomorrow is " << d1.Str() << std::endl; d1-=2; std::cout << "Yesterday was " << d1.Str() << std::endl; std::cout << "There are " << d1 - d2 << " days between " << d1.Str() << " and " << d2.Str() << std::endl; d1="20140701"; std::cout << "Operator = example " << d1.Str() << std::endl; d1+=5; std::cout << "Operator += 5 example " << d1.Str() << std::endl; d1--; std::cout << "Operator -- example " << d1.Str() << std::endl; d1-4; std::cout << "Operator -4 example " << d1.Str() << std::endl; d1+10; std::cout << "Operator +10 example " << d1.Str() << std::endl; std::cout << "CenturyOf() " << d1.CenturyOf() << std::endl; xbString sWorkDate; d1.CTOD( "10/07/60" ); std::cout << "CTOD( '10/07/60' ) " << d1.Str() << std::endl; d1.Set( "19590118" ); std::cout << "Set( '19590118' ) " << d1.Str() << std::endl; std::cout << "CalcRollingCenturyForYear( 95 ) = " << d1.CalcRollingCenturyForYear( 95 ) << std::endl; return 0; }