Cognito Forms logo

Date/Time calculations

Dates are an essential component of any form involved with scheduling activities or recording prior events. Cognito Forms makes it easy to capture real, valid dates, while also providing a rich set of calculation options to validate and manipulate these dates. To capture Date input, just add a Date field and select the Date type. You can also use Calculation fields set to the Date type.

1. Syntax

2. Functions

3. Date/Time formats

4. Entry dates

5. Timespan functions


Syntax

Example

Type

Results for 1/1/2015 and 1/31/2015

=StartDate = EndDate

Equals

1/1/2015 = 1/31/2015 = false

=StartDate != EndDate, or
=StartDate <> EndDate

Not Equals

1/1/2015 != 1/31/2015 = true

=StartDate > EndDate

Greater Than

1/1/2015 > 1/31/2015 = false

=StartDate >= EndDate

Greater Than or Equals

1/1/2015 >= 1/31/2015 = false

=StartDate < EndDate

Less Than

1/1/2015 < 1/31/2015 = true

=StartDate <= EndDate

Less Than or Equals

1/1/2015 <= 1/31/2015 = true

=(EndDate – StartDate).Days

Subtraction

(1/31/2015 - 1/1/2015).Days = 30

=DateTime.Today

Today

DateTime.Today = 5/6/2014

=StartDate.AddDays(10)

Add Days

(1/1/2015).AddDays(10) = 1/11/2015

=StartDate.DayOfWeek

Day of Week

(1/1/2015).DayOfWeek = "Thursday"


Functions

Here is a full list of supported text functions that you can call by typing a period after a Date, Time, or Date & Time field:

Function

 Returns  

Description

AddDays(Number)

 

DateTime                

Returns a new DateTime that adds the specified number of days to the value of this instance.

AddHours(Number)

DateTime

Returns a new DateTime that adds the specified number of hours to the value of this instance.

AddMilliseconds(Number)

DateTime

Returns a new DateTime that adds the specified number of milliseconds to the value of this instance.

AddMinutes(Number)

DateTime

Returns a new DateTime that adds the specified number of minutes to the value of this instance.

AddMonths(Number)

DateTime

Returns a new DateTime that adds the specified number of months to the value of this instance.

AddSeconds(Number)

DateTime

Returns a new DateTime that adds the specified number of seconds to the value of this instance.

AddYears(Number)

DateTime

Returns a new DateTime that adds the specified number of years to the value of this instance.

CompareTo(DateTime)

Number

Compares the value of this instance to a specified DateTime value.  Returns -1, 0, or 1 which means the instance is earlier, equal to, or later than the value, respectively.

Date

DateTime

Returns a new DateTime that represents the date component of this instance.

Day

Number

Returns the day of the month represented by this instance.

DayOfWeek

String

Returns the day of the week represented by this instance.  (i.e. Tuesday or Sunday)

DayOfYear

Number

Returns the day of the year represented by this instance.

Hour

Number

Returns the hour component of the instance.

Millisecond

Number

Returns the millisecond component of the instance.

Minute

Number

Returns the minute component of the instance.

Month

Number

Returns the month component of the instance.

Second

Number

Returns the second component of the instance.

Ticks

Number

Returns the number of ticks (100 nanoseconds) since 1/1/1970 represented by this instance.

Year

Number

Returns the year component of the instance.

 

In addition to these functions, you can access the following by typing DateTime. , followed by the appropriate function. Cognito Forms also supports relative syntax when entering literal date values, such as "Today" and "Tomorrow". Unlike DateTime., you do not need to use an equal sign (=) before inputting a relative value:

                                     

Returns

Name

Description

Yes/No

DateTime.IsLeapYear(Number)

Returns a value indicating whether the specified year is a leap year.

DateTime

"Now" or DateTime.Now

Returns a DateTime object set to the current date and time, expressed as the local time.

DateTime "Yesterday" Returns a DateTime object set to the day before the current date.
DateTime "Tomorrow" Returns a DateTime object set to the day after the current date.
DateTime "Next Week" Returns a DateTime object set to 7 days after the current date.
DateTime "Last Week" Returns a DateTime object set to 7 days before the current date.
DateTime "Next Month" Returns a DateTime object set to a month after the current date.
DateTime "Last Month" Returns a DateTime object set to a month before the current date.
DateTime "2 hours" or "2 hours from now" Returns a DateTime object set to specified number of hours after the current date.
DateTime

"-3 days" or "3 days ago"

Returns a DateTime object set to specified number of days before the current date.
DateTime "Next Year" Returns a new DateTime that adds one year to the current date.
DateTime "Last Year" Returns a new DateTime that subtracts one year from current date.
DateTime "1 year 4 months" Returns a DateTime object set to specified number of years and months after the current date.

DateTime

DateTime.Parse(String)

Converts the string representation of a date and time to its DateTime equivalent.  You can also just specify

DateTime

"Today" or DateTime.Today

Returns a DateTime object set to the current date.

 

Date/Time formats

After inserting a Date field on your form, you can customize the format of the date or time as it appears on other parts of your form. To do this, simply enter a formatted string into a calculation field, like so:

 

 

Here is a full list of possible date/time formats:

Function

Returns

Description

Date.ToString("d/M/yyyy") 6/10/2015 Day from 1 through 31, month, from 1 through 12, full year
Date.ToString("d/MM/yyyy") 6/10/2015 Day from 1 through 31, month, from 01 through 12, full year
=Date.ToString("d/MMM/yyyy") 6/Oct/2015 Day from 1 through 31, abbreviated month, full year
Date.ToString("d/MMMM/yyyy") 6/October/2015 Day from 1 through 31, full month, full year
Date.ToString("dd/MMMM/yyyy") 06/October/2015 Day from 01 through 31, full month, full year
Date.ToString("ddd/MMMM/yyyy") Tue/October/2015 The abbreviated name of the day of the week, full month, full year
Date.ToString("dddd/MMMM/yyyy") Tuesday/October/2015 The full name of the day of the week, full month, full year
Date.ToString("d/MMMM/yy") 6/October/15 Day from 1 through 31, full month, The year, from 00 to 99
Time.ToString("h:m tt") 3:46 PM Hour, using a 12-hour clock from 1 to 12, minute, from 0 through 59, AM/PM designator
Time.ToString("hh:mm tt") 03:46 PM Hour, using a 12-hour clock from 01 to 12, minute, from 00 through 59, AM/PM designator
Time.ToString("H:mm tt") 15:46 PM Hour, using a 24-hour clock from 0 to 23, minute, from 00 through 59, AM/PM designator
Time.ToString("HH:mm tt") 15:46 PM Hour, using a 24-hour clock from 00 to 23, minute, from 00 through 59, AM/PM designator

 

Entry dates

Here are three different date/time values that you can call by typing a period after Entry:

Function

Description

Entry.Created Returns a DateTime set when an entry is saved for the first time via Save & Resume.
Entry.Submitted Returns a DateTime set each time an entry is submitted.
Entry.Updated Returns a DateTime set each time an entry is updated.

 

Timespan functions

Timespan represents a time interval, and is the result of subtracting two Date or Time values.

Type

Returns

Description

Number

Days

Returns the days component of the time interval represented by this instance.

Number

Hours

Returns the hours component of the time interval represented by this instance.

Number

Milliseconds

Returns the milliseconds component of the time interval represented by this instance.

Number

Minutes

Returns the minutes component of the time interval represented by this instance.

Number

Seconds

Returns the seconds component of the time interval represented by this instance.

Number

Ticks

Returns the ticks (100 nanoseconds) component of the time interval represented by this instance.

String

ToString(String)

Converts the value of the TimeSpan instance to its equivalent string using the specified format.  Options: “c”(00:00:00), “g”(0:00:00), “G”(0:00:00:00.0000), “hh\:mm\:ss”(00:00:00).

Number

TotalDays

Returns the value of this instance expressed in whole and fractional days.

Number

TotalHours

Returns the value of this instance expressed in whole and fractional hours.

Number

TotalMilliseconds

Returns the value of this instance expressed in whole and fractional milliseconds.

Number

TotalMinutes

Returns the value of this instance expressed in whole and fractional minutes.

Number

TotalSeconds

Returns the value of this instance expressed in whole and fractional seconds.

Printable View