Date Functions

The Date functions provide methods for creating date objects and getting various properties of the objects. The DateAdd() and DateDiff() functions are convenient for adjusting the desired date based on a specified interval. The Now() function may also be used to get the current time in the locale of the client.

Date

Date( value?, month?, day?, hour?, minute?, second?, millisecond? ) -> returns: Date

Parses a value or set of values to a Date object. See the example snippets below to view various ways this function may be used.

Name Type Description
value Number / String / Date optional The number of milliseconds since January 1, 1970 UTC. Can also be a number representing a year. If a year is specified, then the month and day must also be provided in subsequent parameters. This value may also be an ISO 8601 string to be converted into a date.
month Number optional The month (0-11) where 0 is January and 11 is December.
day Number optional The day of the month (1-31).
hour Number optional The hour of the day (0-23).
minute Number optional The minute of the hour (0-59).
second Number optional The second of the minute (0-59).
millisecond Number optional The millisecond of the second (0-999).
Returns: Date
Example
Create a date with the value of a date field

returns a date object based on a field value

var recordDate = Date($feature.dateField)
Milliseconds since epoch

prints 'Thu Oct 20 2016 11:23:03 GMT-0700 (PDT)'

Date(1476987783555)
Year, month, day

prints 'Tue Jun 02 1987 00:00:00 GMT-0700 (PDT)'

Date(1987,05,02)
ISO 8601 string

prints 'Thu Oct 20 2016 10:41:37 GMT-0700 (PDT)'

Date('2016-10-20T17:41:37+00:0')
With no parameters

prints the current date and time

Date()

DateAdd

DateAdd( date, addValue, units ) -> returns: Date

Adds a specified amount of time in the given units to a date and returns a new date.

Name Type Description
date Date The input date to which to add time.
addValue Number The value to add to the date in the given units.
units String The units of the number to add to the date. The supported unit types include milliseconds, seconds, minutes, hours, days, months, years
Returns: Date
Example
Adds 7 days to the date in the provided field
var startDate = Date($feature.dateField);
var oneWeekLater = DateAdd(startDate, 7, 'days');
return oneWeekLater;


DateDiff

DateDiff( date1, date2, units? ) -> returns: Number

Subtracts two dates, and returns the difference in the specified units.

Name Type Description
date1 Date The date value from which to subtract a second date.
date2 Date The date value to subtract from the first given date.
units String optional The units in which to return the difference of the two given dates. The supported unit types include milliseconds, seconds, minutes, hours, days, months, years. The default value is milliseconds.
Returns: Number
Example
Subtracts two dates and returns the age
var startDate = Date($feature.startDateField);
var endDate = Date($feature.endDateField);
var age = DateDiff(endDate, startDate, 'years');
return age;


Day

Day( dateValue ) -> returns: Number

Returns the day of the month of the given date.

Name Type Description
dateValue Date A date value from which to get the day of the month.
Returns: Number
Example
Gets the day of the month of the current date
Day(Now())

Hour

Hour( dateValue ) -> returns: Number

Returns the hour of the time in the given date (0-23).

Name Type Description
dateValue Date A date value from which to get the hour of the time.
Returns: Number
Example
Gets the hour of the current time
Hour(Now())

Millisecond

Millisecond( dateValue ) -> returns: Number

Returns the millisecond of the time in the date.

Name Type Description
dateValue Date A date value from which to get the millisecond of the time.
Returns: Number
Example
Gets the millisecond of the current time
Millisecond(Now())

Minute

Minute( dateValue ) -> returns: Number

Returns the minute of the time in the given date.

Name Type Description
dateValue Date A date value from which to get the minute of the time.
Returns: Number
Example
Gets the minute of the current time
Minute(Now())

Month

Month( dateValue ) -> returns: Number

Returns the month of the given date. Values range from 0-11 where January is 0 and December is 11.

Name Type Description
dateValue Date A date value from which to get the month.
Returns: Number
Example
Gets the month of the current date
Month(Now())

Now

Now( ) -> returns: date

Returns the current date and time in the locale of the client.

Returns: date
Example
Returns the current date and time

e.g. Mon Oct 24 2016 12:09:34 GMT-0700 (PDT)

Now()

Second

Second( dateValue ) -> returns: Number

Returns the second of the time in the date.

Name Type Description
dateValue Date A date value from which to get the second of the time.
Returns: Number
Example
Gets the second of the current time
Second(Now())

Timestamp

Timestamp( ) -> returns: Date

Since version 1.1

Returns the current date and time in UTC time.

Returns: Date
Example
Returns the current date and time in UTC time

e.g. 29 Mar 2017 08:37:33 pm

Timestamp()

ToLocal

ToLocal( utcDate ) -> returns: Date

Since version 1.1

Converts the given UTC date to a date value in the locale of the client.

Name Type Description
utcDate Date A UTC date value to convert to the locale of the client. This value is assumed to be in UTC time.
Returns: Date
Example
Convert a UTC date to the local time of the client running the app

e.g. Mon Oct 24 2016 00:00:00 GMT-0700 (PDT)

ToLocal(Timestamp())

ToUTC

ToUTC( localDate ) -> returns: Date

Since version 1.1

Converts the given date value from the client's local time to UTC time.

Name Type Description
localDate Date A date value in local time to convert to UTC time. This value is assumed to be in local time.
Returns: Date
Example
Returns the current date and time in UTC

e.g. 29 Mar 2017 08:37:33 pm

// 29 Mar 2017 01:37:33 pm
Now()
// 29 Mar 2017 08:37:33 pm
ToUTC(Now())


Today

Today( ) -> returns: Date

Returns the current date in the locale of the client.

Returns: Date
Example
Returns the current date with time truncated

e.g. Mon Oct 24 2016 00:00:00 GMT-0700 (PDT)

Today()

Weekday

Weekday( dateValue ) -> returns: Number

Returns the day of the week of the given date. Values range from 0-6 where Sunday is 0 and Saturday is 6.

Name Type Description
dateValue Date A date value from which to return the day of the week.
Returns: Number
Example
Returns the day of the week of the current date
Weekday(Now())

Year

Year( dateValue ) -> returns: Number

Returns the year of the given date.

Name Type Description
dateValue Date A date value from which to get the year.
Returns: Number
Example
Gets the year of the current date
Year(Now())


Feedback on this topic?