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. You can use both positive and negative numbers in Date/Time calculations, e.g. =DateTime.Today.AddMonths(-3)

Syntax

Make sure to include quotation marks around date values in calculations (e.g. “12/06/2018”)

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

Quick Tip

Select the function name from the list below to see an example of the function in action!

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.

Relative syntax

In addition to the functions above, 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:

Function Result

Here is a full list of supported relative syntax functions:

Name Returns Description
DateTime.IsLeapYear(Number) Yes/No Returns a value indicating whether the specified year is a leap year.
"Now" or DateTime.Now DateTime Returns a DateTime object set to the current date and time, expressed as the local time.
"Yesterday" DateTime Returns a DateTime object set to the day before the current date.
"Tomorrow" DateTime Returns a DateTime object set to the day after the current date.
"Next Week" DateTime Returns a DateTime object set to 7 days after the current date.
"Last Week" DateTime Returns a DateTime object set to 7 days before the current date.
"Next Month" DateTime Returns a DateTime object set to a month after the current date.
"Last Month" DateTime Returns a DateTime object set to a month before the current date.
"2 hours" or "2 hours from now" DateTime Returns a DateTime object set to specified number of hours after the current date.
"-3 days" or "3 days ago" DateTime Returns a DateTime object set to specified number of days before the current date.
"Next Year" DateTime Returns a new DateTime that adds one year to the current date.
"Last Year" DateTime Returns a new DateTime that subtracts one year from current date.
"1 year 4 months" DateTime Returns a DateTime object set to specified number of years and months after the current date.
DateTime.Parse(String) DateTime Converts the string representation of a date and time to its DateTime equivalent.
"Today" or DateTime.Today DateTime 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.

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.DateCreated Returns a DateTime set when an entry is saved for the first time via Save & Resume.
Entry.DateSubmitted Returns a DateTime set when an entry is submitted for the first time.
Entry.DateUpdated 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. Ex: =(EndTime-StartTime).Hours +":"+(EndTime-StartTime).Minutes

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.
Text Hours:Minutes Returns the hours component and minutes component separated by a colon.
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.
Support