---
schema_version: "1"
id: 9004ca17b7800fc06d4fe2c5b69fbb41e1142dca74be5f3896eda03fdb72ff13
title: toDate()
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/analytics/bi_dev_guide_saql/guides/reference/bi-saql-functions-todate.md
canonical_url: https://developer.salesforce.com/docs/analytics/bi-dev-guide-saql/guide/bi-saql-functions-todate.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: de1ea808025cfdc3e95b699ea18e5cd55ea2184bbf550f7d152888e1296a8fc0
products:
  - Analytics SAQL Developer Guide
  - analytics
---

# toDate()

Converts a string or Unix epoch seconds to a date. Returns a date that can be used in another function such as `daysBetween(​)`. The returned date cannot be used in a filter.

## Syntax

`toDate(string, [”format"])`

If a _format_ argument isn’t provided, the function uses the format `yyyy-MM-dd HH:mm:ss`. _format_ is only valid for converting date string values.

For the allowed formats, see [Date Formats](https://developer.salesforce.com/docs/analytics/bi-dev-guide-saql/guide/bi-date-formats.md).

`toDate(epoch_seconds)`

The _format_ argument is not valid for converting _epoch_seconds_ numerical values.

**note:** Be sure to use the sec_epoch field and not the day_epoch field.

## Example: Display the Exact Date an Opportunity Opened

Suppose that you want to see the exact day that an opportunity opened. Use `toDate()` with `CreatedDate_Year`, `CreatedDate_Month`, `CreatedDate_Day`, and an accepted date format. This query will resemble the example below:

```nolang
q = load "oppty";
q = foreach q generate toDate(CreatedDate_Year + "/" + CreatedDate_Month + "/" + CreatedDate_Day, "yyyy/MM/dd") as CreatedDate;
q = order q by 'CreatedDate';
```

## Example: Display the Number of Days Since an Opportunity Opened

Suppose that you have an opportunity dataset with the account name and the epoch seconds fields:

![Diagram showing the opportunity dataset.](https://developer.salesforce.com/media/bi_dev_guide_saql/bi_saql_date_opp_dataset.png)

You want to see how many days ago an opportunity was opened. Use `daysBetween()` and `now()`. Use `toDate()` to convert the order date epoch seconds to a date format that can be passed to `daysBetween()`.

```nolang
q = load "OpsDates1";

q = foreach q generate Account, daysBetween(toDate(OrderDate_sec_epoch), now()) as 'daysOpened';
```

The resulting data stream displays the number of days since the opportunity was opened.

![Diagram showing the number of days each account has been opened for.](https://developer.salesforce.com/media/bi_dev_guide_saql/bi_saql_date_daysopened.png)

**note:** Because dates are sorted lexicographically, changing the date format also changes the sort order.

**See Also**

- [date()](https://developer.salesforce.com/docs/analytics/bi-dev-guide-saql/guide/bi-saql-functions-datefilter.md)
