Class BirtDateTime

The BirtDateTime class provides functions to determine the amount of time between two dates or times, to add or subtract time spans, and to get parts of a date. Since date spans are just numbers, the application can use normal math on them: multiplication, division, addition, subtraction, and so on. This class is static. The application cannot create instances of the class.

BirtDateTime.addDay

This function returns a new date that results from adding a given number of days to a date. The number of days to add can be negative. In this case, the result is as if that number is subtracted from the date.

Syntax

Date BirtDateTime.addDay( Date date, Number n )

Parameters

date

Date object. A date object that represents the start date.

n

Number object. The number of days to add to the date. Decimals are treated as integers, rounded down to the nearest integer.

Returns

Date object. A date that results from adding the specified number of days to the start date.

Examples

The following example adds days to a specific date:

var startDate = "2008-12-31";

var endDate;

endDate = BirtDateTime.addDay( startDate, 1 ); // returns Jan 01, 2009

endDate = BirtDateTime.addDay( startDate, -1 ); // returns Dec 30, 2008

The following example adds 15 days to each date value in the InvoiceDate field:

BirtDateTime.addDay( row["InvoiceDate"], 15 )

BirtDateTime.addHour

This function returns a new date that results from adding a given number of hours to a date. The number of hours to add can be negative. In this case, the result is as if that number is subtracted from the date.

Syntax

Date BirtDateTime.addHour( Date date, Number n )

Parameters

date

A date object that represents the start date. If a start date does not have a time value, the function assumes the time is midnight (00:00:00).

n

Number object. The number of hours to add to the date.

Returns

Date object. A date that results from adding the specified number of hours to the start date.

Examples

The following example adds hours to a specific date and time:

var startDate = "2008-12-31 15:00:00";

var endDate;

endDate = BirtDateTime.addHour( startDate, 8 ); // returns Dec 31, 2008 11:00 PM

endDate = BirtDateTime.addHour( startDate, -8 ); // returns Dec 31, 2008 7:00 AM

The following example adds 36 hours to each date value in the StartTime field:

BirtDateTime.addHour( row["StartTime"], 36 )

BirtDateTime.addMinute

This function returns a new date that results from adding a given number of minutes to a date. The number of minutes to add can be negative. In this case, the result is as if that number is subtracted from the date.

Syntax

Date BirtDateTime.addMinute( Date date, Number n )

Parameters

date

A date object that represents the start date. If a start date does not have a time value, the function assumes the time is midnight (00:00:00).

n

Number object. The number of minutes to add to the date.

Returns

Date object. A date that results from adding the specified number of minutes to the start date.

Examples

The following example adds minutes to a specific date and time:

var startDate = "2008-12-31 10:30:00";

var endDate;

endDate = BirtDateTime.addMinute( startDate, 45 );

// returns Dec 31, 2008 11:15 AM

endDate = BirtDateTime.addMinute( startDate, -30 );

// returns Dec 31, 2008 10:00 AM

The following example adds 15 minutes to each date value in the StartTime field:

BirtDateTime.addMinute( row["StartTime"], 15 )

BirtDateTime.addMonth

This function returns a new date that results from adding a given number of months to a date. The number of months to add can be negative. In this case, the result is as if that number is subtracted from the date.

This function always returns a valid date. If necessary, the day part of the resulting date is adjusted downward to the last day of the resulting month in

the resulting year. For example, if you add 1 month to Jan 31, 2008, addMonth( ) returns Feb 29, 2008 (2008 is a leap year), not Feb 31, 2008.

the resulting year. For example, if you add 1 month to Jan 31, 2008, addMonth( ) returns Feb 29, 2008 (2008 is a leap year), not Feb 31, 2008.

Syntax

Date BirtDateTime.addMonth( Date date, Number n )

Parameters

date

Date object. A date object that represents the start date.

n

Number object. The number of months to add to the date. Decimals are treated as integers, rounded down.

Returns

Date object. A date that results from adding the specified number of months to the start date.

Examples

The following example adds months to a specific date:

var startDate = "2008-12-31";

var endDate;

endDate = BirtDateTime.addMonth( startDate, 1 ); // returns Jan 31, 2009

endDate = BirtDateTime.addMonth( startDate, -1 ); // returns Nov 30, 2008

endDate = BirtDateTime.addMonth( startDate, 2 ); // returns Feb 28, 2009

The following example adds 10 months to each date value in the InvoiceDate field:

BirtDateTime.addMonth( row["InvoiceDate"], 10 )

BirtDateTime.addQuarter

This function returns a new date that results from adding a given number of quarters to a date. A quarter is equal to three months. For example, if you add 2 quarters to Sep 10, 2008, the function returns Mar 10, 2009. The number of quarters to add can be negative. In this case, the result is as if that number is subtracted from the date.

Syntax

Date BirtDateTime.addQuarter( Date date, Number n )

Parameters

date

A date object that represents the start date.

n

Number object. The number of quarters to add to the date.

Returns

Date object. A date that results from adding the specified number of quarters to the start date.

Examples

The following example adds quarters to a specific date:

var startDate = "2008-12-15";

var endDate;

endDate = BirtDateTime.addQuarter( startDate, 1 ); //returns Mar 15, 2009

endDate = BirtDateTime.addQuarter( startDate, -2 ); //returns Jun 15, 2008

The following example adds 2 quarters to each date value in the ForecastClosing field:

BirtDateTime.addQuarter( row["ForecastClosing"], 2 )

BirtDateTime.addSecond

This function returns a new date that results from adding a given number of seconds to a date. The number of seconds to add can be negative. In this case, the result is as if that number is subtracted from the date.

Syntax

Date BirtDateTime.addSecond( Date date, Number n )

Parameters

date

A date object that represents the start date. If a start date does not have a time value, the function assumes the time is midnight (00:00:00).

n

Number object. The number of seconds to add to the date.

Returns

Date object. The result from adding the specified number of seconds to the start date.

Examples

The following example adds seconds to a specific date and time:

var startDate = "2008-12-31 10:30:00";

var endDate;

endDate = BirtDateTime.addSecond( startDate, 10000 );

// returns returns Dec 31, 2008 1:16:40 PM

endDate = BirtDateTime.addSecond( startDate, -10000 );

// returns Dec 31, 2008 7:43:20 AM

The following example adds 6000 seconds to each date value in the StartTime field:

BirtDateTime.addSecond( row["StartTime"], 6000 )

BirtDateTime.addWeek

This function returns a new date that results from adding a given number of weeks to a date. The number of weeks to add can be negative. In this case, the result is as if that number is subtracted from the date.

Syntax

Date BirtDateTime.addWeek( Date date, Number n )

Parameters

date

A date object that represents the start date.

n

Number Object. The number of weeks to add to the date.

Returns

Date object. A date that results from adding the specified number of weeks to the start date.

Examples

The following example adds weeks to a specific date:

var startDate = "2008-12-31";

var endDate;

endDate = BirtDateTime.addWeek( startDate, 1 ); // returns Jan 07, 2009

endDate = BirtDateTime.addWeek( startDate, -1 ); // returns Dec 24, 2008

The following example adds 2 weeks to each date value in the InvoiceDate field:

BirtDateTime.addWeek( row["InvoiceDate"], 2 )

BirtDateTime.addYear

This function returns a new date that results from adding a given number of years to a date. The number of years to add can be negative. In this case, the result is as if that number is subtracted from the date.

Syntax

Date BirtDateTime.addYear( Date date, Number n )

Parameters

date

A date object that represents the start date.

n

Number object.The number of years to add to the date. Decimals are treated as integers, rounded down.

Returns

A date that results from adding the specified number of years to the start date.

Examples

The following example adds years to a specific date:

var startDate = "2008-12-31";

var endDate;

endDate = BirtDateTime.addYear( startDate, 1 ); // returns Dec 31, 2009

endDate = BirtDateTime.addYear( startDate, -1 ); // returns Dec 31, 2007

The following example adds 5 years to each date value in the HireDate field:

BirtDateTime.addYear( row["HireDate"], 5 )

BirtDateTime.day

This function returns a number from 1 to 31 that represents the day of the month for a given date. For example, if the date is 2009-01-19, the function returns 19.

Syntax

integer BirtDateTime.day( Date date )

Parameter

date

Date object. A date object from which to get the day.

Returns

An integer that represents the day of the month for a given date.

Examples

The following example returns the day of the month for each value in the ShipDate field:

BirtDateTime.day( row["ShipDate"] )

The following example returns the day of the month for the current date:

BirtDateTime.day( (BirtDateTime.today())

BirtDateTime.diffDay

This function returns the number of days between two dates. A day is calculated by subtracting the day value of the first date from the day value of the second date. For example, if the first date is 2008-12-30 23:59:59 and the second date is 2008-12-31 00:00:00, the function returns 1 day, even though only one second separates the two dates.

Syntax

integer BirtDateTime.diffDay( Date date1, Date date2 )

Parameters

date1

A date object that represents the start of the time span.

date2

A date object that represents the end of the time span.

Returns

Integer. The number of days between two dates.

Examples

The following example returns the number of days between specific dates:

BirtDateTime.diffDay( "2009-01-01", "2009-04-15" ) // returns 104

The following example returns the number of days from the current date to the end of 2009:

BirtDateTime.diffDay( BirtDateTime.today(), "2009-12-31" )

The following example returns the number of days between values in the InvoiceDate and PaymentDate fields:

BirtDateTime.diffDay( row["InvoiceDate"], row["PaymentDate"] )

BirtDateTime.diffHour

This function returns the number of hours between two dates. An hour is calculated by subtracting the hour value of the first date from the hour value of the second date. For example, if the first date is 2009-01-31 1:30:00 and the second date is 2009-01-31 2:00:00, the function returns 1 hour, even though only 30 minutes separates the two dates. If a date does not have a time value, the function assumes the time is midnight (00:00:00).

Syntax

integer BirtDateTime.diffHour( Date date1, Date date2 )

Parameters

date1

A date object that represents the start of the time span.

date2

A date object that represents the end of the time span.

Returns

Integer. The number of hours between two dates.

Examples

The following example returns the number of hours between specific dates:

BirtDateTime.diffHour( "2009-01-31 12:00:00", "2009-02-01 12:00:00")

// returns 24

BirtDateTime.diffHour( "2009-01-31", "2009-02-01 12:00:00")

// returns 36. Because the first date does not have a time, midnight is assumed.

The following example returns the number of hours from the current date and time to the end of 2009:

BirtDateTime.diffHour( BirtDateTime.now(), "2009-12-31" );

The following example returns the number of hours between values in the StartTime and FinishTime fields:

BirtDateTime.diffHour( row["StartTime"], row["FinishTime"] )

BirtDateTime.diffMinute

This function returns the number of minutes between two dates. A minute is calculated by subtracting the minute value of the first date from the minute value of the second date. For example, if the first date is 2009-01-31 1:30:59 and the second date is 2009-01-31 1:31:00, the function returns 1 minute, even though only one second separates the two dates. If a date does not have a time value, the function assumes the time is midnight (00:00:00).

Syntax

integer BirtDateTime.diffMinute( Date date1, Date date2 )

Parameters

date1

A date object that represents the start of the time span.

date2

A date object that represents the end of the time span.

Returns

Integer. The number of minutes between two dates.

Examples

The following example returns the number of minutes between specific dates:

BirtDateTime.diffMinute( "2009-01-31 1:30:30", "2009-01-31 10:45:00")

// returns 555

BirtDateTime.diffMinute( "2009-01-31", "2009-01-31 12:00:00")

// returns 720. Because the first date does not have a time, midnight is assumed.

The following example returns the number of minutes from the current date and time to the beginning of 2010:

BirtDateTime.diffMinute( BirtDateTime.now(), "2010-01-01" )

The following example returns the number of minutes between values in the StartTime and FinishTime fields:

BirtDateTime.diffMinute( row["StartTime"], row["FinishTime"] )

BirtDateTime.diffMonth

This function returns the number of months between two dates. A month is calculated by subtracting the month value of the first date from the month value of the second date. For example, if the first date is 2009-01-31 and the second date is 2009-02-01, the function returns 1 month, even though only one day separates the two dates.

Syntax

integer BirtDateTime.diffMonth( Date date1, Date date2 )

Parameters

date1

A date object that represents the start of the time span.

date2

A date object that represents the end of the time span.

Returns

Integer. The number of months between two dates.

Examples

The following example returns the number of months between specific dates:

BirtDateTime.diffMonth( "2009-01-31", "2009-04-15") // returns 3

The following example returns the number of months from the current date to the beginning of 2010:

BirtDateTime.diffMonth( BirtDateTime.today(), "2010-01-01" );

The following example returns the number of months between values in the ProjectStart and ProjectComplete fields:

BirtDateTime.diffMonth( row["ProjectStart"], row["ProjectComplete"] )

BirtDateTime.diffQuarter

This function returns the number of quarters between two dates. A quarter is calculated by subtracting the month value of the first date from the month value of the second date. A difference of 3 months is equal to 1 quarter. For example, if the first date is 2009-01-31 and the second date is 2009-07-01, the function returns 2. If the first date is 2009-01-01 and the second date is 2009-09-30, the function also returns 2.

Syntax

integer BirtDateTime.diffQuarter( Date date1, Date date2 )

Parameters

date1

A date object that represents the start of the time span.

date2

A date object that represents the end of the time span.

Returns

Integer. The number of quarters between two dates.

Examples

The following example returns the number of quarters between specific dates:

BirtDateTime.diffQuarter( "2009-01-31", "2009-04-15") // returns 1

The following example returns the number of quarters from the current date to the beginning of 2010:

BirtDateTime.diffQuarter( BirtDateTime.today(), "2010-01-01" );

The following example returns the number of quarters between values in the ProjectStart and ProjectComplete fields:

BirtDateTime.diffQuarter( row["ProjectStart"], row["ProjectComplete"] )

BirtDateTime.diffSecond

This function returns the number of seconds between two dates. If a date does not have a time value, the function assumes the time is midnight (00:00:00).

Syntax

integer BirtDateTime.diffSecond( date1, date2 )

Parameters

date1

A date object that represents the start of the time span.

date2

A date object that represents the end of the time span.

Returns

Integer. The number of seconds between two dates.

Examples

The following example returns the number of seconds between specific dates:

BirtDateTime.diffSecond( "2009-01-31 11:30:30", "2009-01-31 12:00:00")

// returns 1770

BirtDateTime.diffSecond( "2009-01-31", "2009-01-31 1:00:00")

// returns 3600. Because the first date does not have a time, midnight is assumed.

The following example returns the number of seconds from the current date and time to the beginning of 2010:

BirtDateTime.diffSecond( BirtDateTime.now(), "2010-01-01" )

The following example returns the number of seconds between values in the StartTime and FinishTime fields:

BirtDateTime.diffSecond( row["StartTime"], row["FinishTime"] )

BirtDateTime.diffWeek

This function returns the number of weeks between two dates. A week is calculated by subtracting the week number of the first date from the week number of the second date. For example, if the first date is 2009-01-02 (week 1 of the year) and the second date is 2009-01-05 (week 2 of the year), the function returns one week, even though only three days separates the two dates.

Syntax

integer BirtDateTime.diffWeek( Date date1, Date date2 )

Parameters

date1

A date object that represents the start of the time span.

date2

A date object that represents the end of the time span.

Returns

Integer. The number of weeks between two dates.

Examples

The following example returns the number of weeks between specific dates:

BirtDateTime.diffWeek( "2009-01-01", "2009-02-01") // returns 5

The following example returns the number of weeks from the current date to the beginning of 2010:

BirtDateTime.diffWeek( BirtDateTime.today(), "2010-01-01" );

The following example returns the number of weeks between values in the ProjectStart and ProjectComplete fields:

BirtDateTime.diffWeek( row["ProjectStart"], row["ProjectComplete"] )

BirtDateTime.diffYear

This function returns the number of years between two dates. A year is calculated by subtracting the year value of the first date from the year value of the second date. For example, if the first date is 2009-01-01 and the second date is 2009‑12‑31, the function returns 0. If the first date is 2008-11-15 and the second date is 2009‑01‑15, the function returns 1.

Syntax

integer BirtDateTime.diffYear( Date date1, Date date2 )

Parameters

date1

A date object that represents the start of the time span.

date2

A date object that represents the end of the time span.

Returns

Integer. The number of years between two dates.

Examples

The following example returns the number of years between specific dates:

BirtDateTime.diffYear( "2005-01-01", "2009-06-01") // returns 4

The following example returns the number of years between each value in the HireDate field and the current date:

BirtDateTime.diffYear( row["HireDate"], BirtDateTime.today() )

BirtDateTime.firstDayOfMonth

This function returns the date for the first day of the month in which the input date falls.

Syntax

Date BirtDateTime.firstDayOfMonth( Date date)

Parameters

date

A date object from which to get the month.

Returns

Date object. The first day of the month for a given date.

Examples

The following example shows the month values returned for a specific date:

BirtDateTime.firstDayOfMonth( "2009-02-15" ) // returns 2009-02-01

BirtDateTime.firstDayOfQuarter

This function returns the date for the first day of the quarter in which the input date falls.

Syntax

Date BirtDateTime.firstDayOfQuarter( Date date)

Parameters

date

A date object from which to get the quarter.

Returns

Date object. The first day of the quarter for a given date.

Examples

The following example shows the quarter values returned for a specific date:

BirtDateTime.firstDayOfQuarter( "2009-02-15" )

// returns 2009-01-01

// returns 2009-01-01

BirtDateTime.firstDayOfWeek

This function returns the date for the first day of the week in which the input date falls, which is always a Sunday.

Syntax

Date BirtDateTime.firstDayOfWeek( Date date)

Parameters

date

A date object from which to get the week.

Returns

Date object. The first day of the week for a given date.

Examples

The following example shows the week values returned for a specific date:

BirtDateTime.firstDayOfWeek( "2009-02-15" ) // returns 2009-02-15

BirtDateTime.firstDayOfYear

This function returns the date for the first day of the year in which the input date falls.

Syntax

Date BirtDateTime.firstDayOfYear( Date date)

Parameters

date

A date object from which to get the year.

Returns

Date object. The first day of the year for a given date.

Examples

The following example shows the year values returned for a specific date:

BirtDateTime.firstDayOfYear( "2009-02-15" ) // returns 2009-01-01

BirtDateTime.month

This function returns the month for a given date. Depending on the option you specify, the function returns the month as a number, the full month name, or the abbreviated month name. The month name is locale-specific.

Syntax

string BirtDateTime.month( Date date, integer option )

Parameters

date

A date object from which to get the month.

option

Integer. Optional. A number that represents the month format to return. Use one of the following values:

1 to get the month as a number from 1 to 12

2 to get the full month name, for example, January

3 to get the abbreviated month name, for example, Jan

If you omit this argument, the function returns the month as a number.

Returns

String. The month for a given date.

Examples

The following example shows the month values returned for a specific date:

BirtDateTime.month( "2009-02-15" ) // returns 2

BirtDateTime.month( "2009-02-15", 2 ) // returns February

BirtDateTime.month( "2009-02-15", 3) // returns Feb

The following example returns the month for the current date:

BirtDateTime.month( (BirtDateTime.today() )

BirtDateTime.now

This function returns the current date and time, for example, Feb 2, 2009 2:05 PM.

Syntax

Date BirtDateTime.now( )

Returns

Date object. The current date and time.

Example

The following example returns the number of hours from the current date and time to Christmas:

BirtDateTime.diffHour( BirtDateTime.now(), "2009-12-25" )

BirtDateTime.quarter

This function returns a number from 1 to 4 that represents the quarter in which a given date falls. The first quarter begins on January 1.

Syntax

integer BirtDateTime.quarter( Date date )

Parameter

date

A date object from which to get the quarter number.

Returns

Integer. The quarter number for a given date.

Examples

The following example shows the quarter numbers returned for specific dates:

BirtDateTime.quarter( "2009-02-15" ) // returns 1

BirtDateTime.quarter( "2009-07-10" ) // returns 3

The following example returns the quarter number for each value in the CloseDate field:

BirtDateTime.quarter( row["CloseDate"] )

The following example returns the quarter number for the current date:

BirtDateTime.quarter( (BirtDateTime.today() )

BirtDateTime.today

This function returns the current date that includes a time value of midnight, for example, Feb 2, 2009 12:00 AM.

Syntax

Date BirtDateTime.today( )

Returns

Date object. The current date with a time value of midnight.

Examples

The following example returns the number of days from the current date to Christmas:

BirtDateTime.diffDay( BirtDateTime.today(), "2009-12-25" )

The following example calculates the number of years from each value in the HireDate data field to the current date.

BirtDateTime.diffDay( BirtDateTime.today(), "2009-12-25" )

BirtDateTime.week

This function returns a number from 1 to 52 that represents the week number for a given date.

Syntax

integer BirtDateTime.week( date )

Parameter

date

A date object from which to get the week number.

Returns

Integer. The week number for a given date.

Examples

The following example shows the week numbers returned for specific dates:

BirtDateTime.week( "2009-02-15" ) // returns 8

BirtDateTime.week( "2009-10-12" ) // returns 42

The following example returns the week number for each value in the CloseDate field:

BirtDateTime.week( row["CloseDate"] )

The following example returns the week number for the current date:

BirtDateTime.week( (BirtDateTime.today() )

BirtDateTime.weekDay

This function returns the day of the week for a given date. Depending on the option you specify, the function returns the day of the week as a number, the full weekday name, or the abbreviated weekday name. The weekday name is locale‑specific.

Syntax

string BirtDateTime.weekDay( Date date, integer option )

Parameters

date

A date object from which to get the day of the week.

option

Integer. Optional. A number that represents the format to return. Use one of the following values:

1 to get the day as a number from 1 (Sunday) to 7 (Saturday)

2 to get the day as a number from 1 (Monday) to 7 (Sunday)

3 to get the day as a number from 0 (Monday) to 6 (Sunday)

4 to get the full weekday name, for example, Wednesday

5 to get the abbreviated weekday name, for example, Wed

If you omit this argument, the function assumes option 1.

Returns

String. The day of the week for a given date.

Examples

The following example shows the week day values returned for a specific date:

BirtDateTime.weekDay( "2009-02-02" ) // returns 2

BirtDateTime.weekDay( "2009-02-02", 2 ) // returns 1

BirtDateTime.weekDay( "2009-02-02", 4 ) // returns Monday

The following example returns the day of the week for the current date:

BirtDateTime.weekDay( (BirtDateTime.today() )

BirtDateTime.year

This function returns the four-digit year value for a given date. For example, if the date is 2009-01-19, the function returns 2009.

Syntax

integer BirtDateTime.year( date )

Parameter

date

A date object from which to get the year.

Returns

Integer. The four-digit year value for a given date.

Examples

The following example returns the year for each value in the HireDate field:

BirtDateTime.year( row["HireDate"] )

The following example returns the year for the current date:

BirtDateTime.year( (BirtDateTime.today())