> For the complete documentation index, see [llms.txt](https://boxlang.ortusbooks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/temporal/datecompare.md).

# DateCompare

Compares the difference between two dates - returning 0 if equal, -1 if date2 is less than date1 and 1 if the inverse

## Method Signature

```
DateCompare(date1=[any], date2=[any], datepart=[string])
```

### Arguments

| Argument   | Type     | Required | Description                                                                 | Default |
| ---------- | -------- | -------- | --------------------------------------------------------------------------- | ------- |
| `date1`    | `any`    | `true`   | The reference date object                                                   |         |
| `date2`    | `any`    | `true`   | The date which to compare against date1                                     |         |
| `datepart` | `string` | `false`  | The precision to compare down to. Accepts y, yyyy, m, d, h, n, s (default). |         |

## Examples

### Compare Two Dates by Year

[Run Example](https://try.boxlang.io/?code=eJxLSSxJdc7PLUgsStVQUDI00jc20DcyMDRV0gHzDIzgvEogUFLQtOYCAGFDDFs%3D)

```java
dateCompare( "12/30/2015", "12/02/2015", "yyyy" );

```

Result: 0

### Compare Two Dates by Day

Returns 1 because date1 is greater than date 2

[Run Example](https://try.boxlang.io/?code=eJxLSSxJdc7PLUgsStVQUDI00jc20DcyMDRV0gHzDIzgvBQlBU1rLgA4GArb)

```java
dateCompare( "12/30/2015", "12/02/2015", "d" );

```

Result: 1

### Member function example

[Run Example](https://try.boxlang.io/?code=eJxLMVSwVUguSk0sSXUBYg0FJSMDIxMlHQUlA0MoqaBpzZVihFOZEYg0NIEoM9RLzs8tSCwCqgBqAYoAAGSPFq0%3D)

```java
d1 = createDate( "2024", "01", "01" );
d2 = createDate( "2024", "02", "14" );
d1.compare( d2 );

```

Result: -1

### Additional Examples

[Run Example](https://try.boxlang.io/?code=eJwrL8osSfUvLSkoLdFQSEksSXXOzy1ILErVUMjLL9fQ1FFQMjTUNzTQN7S0NFJS0FRQU1BS0HABqjNUyCxWyAEyihRKMhLzwHqNNG2SiuyAyqy5ynGai2wgAeNTC0sTcxRK8sk1HOwFqKkIQxOLcjJRXA02EwBD4Ewp)

```java
writeOutput( dateCompare( now(), "11/10/1992" ) & " (Date1 is later than date2)<br>" );
writeOutput( dateCompare( "11/10/1992", "11/10/1992" ) & " (Date1 is equal to date2)<br>" );
writeOutput( dateCompare( "11/10/1992", now() ) & " Date1 is earlier than date2" );

```

[Run Example](https://try.boxlang.io/?code=eJx1zsEKwjAMBuD7niL0IC2IW3YbUy969xmiLWywrTOmjL29ncrcEA%2BBBPJ%2FiYUD3NiRuHMsDaMj1tD5QRswW2h9J9VitjTOE5gysRjzCjHFLMWiyFWZDFyLuwTpg2iwu5Nve%2BIoL7dieAMKppMI9QOa2DBIRV28IC7fX%2FmoJj%2F779kPomfF3QM1IP5tmBlZC%2FhLfAXipl598gKe58xUzw%3D%3D)

```java
d = createDate( year( now() ), month( now() ), day( now() ) );
d1 = "11/10/1992";
writeOutput( d.Compare( "11/10/1992" ) & " Date1 is later than date2<br>" );
0;
writeOutput( d.Compare( d ) & " (Date1 is equal to date2)<br>" );
writeOutput( d1.Compare( d ) & " Date1 is earlier than date2" );

```

## Related

* [ClearTimezone](/boxlang-language/reference/built-in-functions/temporal/cleartimezone.md)
* [CreateDate](/boxlang-language/reference/built-in-functions/temporal/createdate.md)
* [CreateDateTime](/boxlang-language/reference/built-in-functions/temporal/createdatetime.md)
* [CreateODBCDate](/boxlang-language/reference/built-in-functions/temporal/createodbcdate.md)
* [CreateODBCDateTime](/boxlang-language/reference/built-in-functions/temporal/createodbcdatetime.md)
* [CreateODBCTime](/boxlang-language/reference/built-in-functions/temporal/createodbctime.md)
* [CreateTime](/boxlang-language/reference/built-in-functions/temporal/createtime.md)
* [CreateTimeSpan](/boxlang-language/reference/built-in-functions/temporal/createtimespan.md)
* [DateAdd](/boxlang-language/reference/built-in-functions/temporal/dateadd.md)
* [DateConvert](/boxlang-language/reference/built-in-functions/temporal/dateconvert.md)
* [DateDiff](/boxlang-language/reference/built-in-functions/temporal/datediff.md)
* [DateFormat](/boxlang-language/reference/built-in-functions/temporal/dateformat.md)
* [DatePart](/boxlang-language/reference/built-in-functions/temporal/datepart.md)
* [dateSetDay](/boxlang-language/reference/built-in-functions/temporal/datesetday.md)
* [dateSetHour](/boxlang-language/reference/built-in-functions/temporal/datesethour.md)
* [dateSetMinute](/boxlang-language/reference/built-in-functions/temporal/datesetminute.md)
* [dateSetMonth](/boxlang-language/reference/built-in-functions/temporal/datesetmonth.md)
* [dateSetSecond](/boxlang-language/reference/built-in-functions/temporal/datesetsecond.md)
* [dateSetYear](/boxlang-language/reference/built-in-functions/temporal/datesetyear.md)
* [DateTimeFormat](/boxlang-language/reference/built-in-functions/temporal/datetimeformat.md)
* [Day](/boxlang-language/reference/built-in-functions/temporal/day.md)
* [DayOfWeek](/boxlang-language/reference/built-in-functions/temporal/dayofweek.md)
* [DayOfWeekAsString](/boxlang-language/reference/built-in-functions/temporal/dayofweekasstring.md)
* [DayOfWeekShortAsString](/boxlang-language/reference/built-in-functions/temporal/dayofweekshortasstring.md)
* [DayOfYear](/boxlang-language/reference/built-in-functions/temporal/dayofyear.md)
* [DaysInMonth](/boxlang-language/reference/built-in-functions/temporal/daysinmonth.md)
* [DaysInYear](/boxlang-language/reference/built-in-functions/temporal/daysinyear.md)
* [FirstDayOfMonth](/boxlang-language/reference/built-in-functions/temporal/firstdayofmonth.md)
* [GetNumericDate](/boxlang-language/reference/built-in-functions/temporal/getnumericdate.md)
* [GetTime](/boxlang-language/reference/built-in-functions/temporal/gettime.md)
* [GetTimezone](/boxlang-language/reference/built-in-functions/temporal/gettimezone.md)
* [GetTimezoneInfo](/boxlang-language/reference/built-in-functions/temporal/gettimezoneinfo.md)
* [Hour](/boxlang-language/reference/built-in-functions/temporal/hour.md)
* [Millisecond](/boxlang-language/reference/built-in-functions/temporal/millisecond.md)
* [Minute](/boxlang-language/reference/built-in-functions/temporal/minute.md)
* [Month](/boxlang-language/reference/built-in-functions/temporal/month.md)
* [MonthAsString](/boxlang-language/reference/built-in-functions/temporal/monthasstring.md)
* [MonthShortAsString](/boxlang-language/reference/built-in-functions/temporal/monthshortasstring.md)
* [Nanosecond](/boxlang-language/reference/built-in-functions/temporal/nanosecond.md)
* [Now](/boxlang-language/reference/built-in-functions/temporal/now.md)
* [Offset](/boxlang-language/reference/built-in-functions/temporal/offset.md)
* [ParseDateTime](/boxlang-language/reference/built-in-functions/temporal/parsedatetime.md)
* [Quarter](/boxlang-language/reference/built-in-functions/temporal/quarter.md)
* [Second](/boxlang-language/reference/built-in-functions/temporal/second.md)
* [SetTimezone](/boxlang-language/reference/built-in-functions/temporal/settimezone.md)
* [TimeFormat](/boxlang-language/reference/built-in-functions/temporal/timeformat.md)
* [TimeUnits](/boxlang-language/reference/built-in-functions/temporal/timeunits.md)
* [Week](/boxlang-language/reference/built-in-functions/temporal/week.md)
* [Year](/boxlang-language/reference/built-in-functions/temporal/year.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/temporal/datecompare.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
