多维表格
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

177 lines
4.9 KiB

---
title: 'Date functions'
description: 'This article explains various date functions that can be used in formula fields.'
tags: ['Fields', 'Field types', 'Formula', 'Date & Time']
keywords: ['Fields', 'Field types', 'Formula', 'Date & Time', 'Create formula field', 'Date functions']
---
This cheat sheet provides a quick reference guide for various date functions commonly used in data analysis and programming. Each function is accompanied by its syntax, a sample usage, and a brief description.
## DATETIME_DIFF
The DATETIME_DIFF function calculates the difference between two dates in various units.
#### Syntax
```plaintext
DATETIME_DIFF(date1, date2, ["milliseconds" | "ms" | "seconds" | "s" | "minutes" | "m" | "hours" | "h" | "days" | "d" | "weeks" | "w" | "months" | "M" | "quarters" | "Q" | "years" | "y"])
```
#### Sample
```plaintext
DATETIME_DIFF("2022/10/14", "2022/10/15", "seconds") => -86400
```
#### Remark
This function compares two dates and returns the difference in the specified unit. Positive integers indicate that the second date is in the past compared to the first, and vice versa for negative values.
---
## DATEADD
The DATEADD function adds a specified value to a date or datetime.
#### Syntax
```plaintext
DATEADD(date | datetime, value, ["day" | "week" | "month" | "year"])
```
#### Sample
```plaintext
DATEADD('2022-03-14', 1, 'day') => 2022-03-15
DATEADD('2022-03-14', 1, 'week') => 2022-03-21
DATEADD('2022-03-14', 1, 'month') => 2022-04-14
DATEADD('2022-03-14', 1, 'year') => 2023-03-14
```
#### Conditional Example
```plaintext
IF(NOW() < DATEADD(date, 10, 'day'), "true", "false") => If the current date is less than the specified date plus 10 days, it returns true. Otherwise, it returns false.
```
#### Remark
This function supports date and datetime fields and can handle negative values.
---
## NOW
The NOW function returns the current time and day.
#### Syntax
```plaintext
NOW()
```
#### Sample
```plaintext
NOW() => 2022-05-19 17:20:43 (current date & time)
```
#### Conditional Example
```plaintext
IF(NOW() < date, "true", "false") => If the current date is less than the specified date, it returns true. Otherwise, it returns false.
```
#### Remark
This function provides the current time and day, supporting datetime fields and negative values.
---
## WEEKDAY
The WEEKDAY function returns the day of the week as an integer.
#### Syntax
```plaintext
WEEKDAY(date, [startDayOfWeek])
```
#### Sample
```plaintext
WEEKDAY(NOW()) => If today is Monday, it returns 0.
WEEKDAY(NOW(), "sunday") => If today is Monday, it returns 1.
```
#### Remark
Returns the day of the week as an integer between 0 and 6 (inclusive), with Monday as the default start day. The start day of the week can be optionally changed by specifying it as the second argument.
---
## DATESTR
The DATESTR function converts a date or datetime field into a string in "YYYY-MM-DD" format.
#### Syntax
```plaintext
DATESTR(date | datetime)
```
#### Sample
```plaintext
DATESTR('2022-03-14') => 2022-03-14
DATESTR('2022-03-14 12:00:00') => 2022-03-14
```
#### Remark
This function converts a date or datetime field into a string in "YYYY-MM-DD" format, ignoring the time part.
---
## DAY
The DAY function returns the day of the month as an integer.
#### Syntax
```plaintext
DAY(date | datetime)
```
#### Sample
```plaintext
DAY('2022-03-14') => 14
DAY('2022-03-14 12:00:00') => 14
```
#### Remark
This function returns the day of the month as an integer between 1 and 31 (inclusive). Note that the day information retrieved is based on the timezone of the server (GMT by default). If the browser timezone is different from the server timezone, the day value may differ.
---
## MONTH
The MONTH function returns the month of the year as an integer.
#### Syntax
```plaintext
MONTH(date | datetime)
```
#### Sample
```plaintext
MONTH('2022-03-14') => 3
MONTH('2022-03-14 12:00:00') => 3
```
#### Remark
This function returns the month of the year as an integer between 1 and 12 (inclusive). Note that the month information retrieved is based on the timezone of the server (GMT by default). If the browser timezone is different from the server timezone, the month value may differ.
---
## HOUR
The HOUR function returns the hour of the day as an integer.
#### Syntax
```plaintext
HOUR(datetime)
```
#### Sample
```plaintext
HOUR('2022-03-14 12:00:00') => 12
```
#### Remark
This function returns the hour of the day as an integer between 0 and 23 (inclusive). Hour information retrieved is based on a 24-hour clock & will be based on the timezone of the server (GMT by default). Note that, if browser timezone is different from the server timezone, the hour value may differ.
---
## Related Articles
- [Numeric and Logical Operators](015.operators.md)
- [Numeric Functions](020.numeric-functions.md)
- [String Functions](030.string-functions.md)
- [Conditional Expressions](050.conditional-expressions.md)