Skip to main content
DateTime is a complex data type representing a timestamp with both date and time elements on ISO format. The default value is now. Format: YYYY-MM-DD
This will print today’s date and the current time.
You can create arrays of any data type to store more than one value at the same time like this: String[] s1;. For a given array, all items must be of the same type. Read more about looping and accessing array items in the fundamentals section.

Constructors

DateTime DateTime(DateTime dt)

Pass a DateTime object to copy into a new object.

DateTime DateTime(Integer year, Integer month, Integer mday, Integer hour, Integer min, Integer sec)

Specify all elements of a DateTime individually. The constructor automatically calculates the weekday.

DateTime DateTime(String p0)

Pass a String containing date and time on the format of one of the listed formats. The constructor will parse the text and create a DateTime object.
  • YYYY-MM-DD HH:MM:SS
  • YYYY-MM-DD HH:MM - automatically sets sec = 0
  • YYYYMMDDHHMMSS - mysql.timestamp
  • YYYY-MM-DD - automatically sets the time to 23:59:59 or 0:0:0 depending on endOfDay setting
  • an empty string or “0” - sets stamp to Jan 1. 1970 00:00:00 and isNull()
  • YYYY-MM-DD HH:MM:SS:XXX

Timestamps as strings

String toString()

toString() is one of the most frequently used methods, typically when you are going to output something. It returns a string representation of a DateTime object using standard formatting.
This is the equivalent of:

String toString(String format)

A variant of toString() that takes a string with formatting codes. You can also include white-space and punctuation marks.
This will print the current time as hh:mm in 12-hour mode and indicating whether it is am or pm.

String toString(String format, String months, String weekDays)

A variant of toString() that takes a string with formatting codes plus comma-separated lists of month and weekday names in your preferred language.
If you don’t include codes MONTH, WDAY, or both - use toString(String format) instead. If you include only 1 of them, send an empty string for the one you don’t use.

String toString(Integer mode, Integer language, Bool 24HourMode)

A variant of toString() that takes codes for mode and language as Integers and a boolean indicator for 12- or 24-hour clock.
  • true: use time in 24-hour mode
  • false: use time in 12-hour mode
This will print today’s date and the current time formatted similar to toString() without arguments, but with the month as a 3-letter abbreviation. Mode: [0-16], see end of this section Try the following snippet to view output of all modes.
Languages:

Setting and updating date and time

Date and time values are set relative to when the DateTime object was created.
  • A positive increment indicates sometime in the future.
  • A negative value indicates sometime in the past.
The amount must be provided as an Integer input parameter.

DateTime addDay(Integer num)

addDay() will adjust the currently set date with the given number of days. The parameter granularity is days.

DateTime addMonth(Integer num)

addMonth() will adjust the currently set date with the given number of months. The parameter granularity is months.

DateTime addYear(Integer num)

addYear() will adjust the currently set date with the given number of years. The parameter granularity is years.

DateTime addHour(Integer num)

addHour() will adjust the currently set time with the given number of hours. The parameter granularity is hours.

DateTime addMin(Integer num)

addMin() will adjust the currently set time with the given number of minutes. The parameter granularity is minutes.

DateTime addSec(Integer num)

addSec() will adjust the currently set time with the given number of seconds. The parameter granularity is days.

Void setTime(Time theTime)

setTime() sets the time-part of a DateTime by passing a Time object.
This will output the current date and time fixed to 13 o’clock.

Retrieving date and time details

Integer getMDay()

getMDay() returns the day of the month as an Integer [1-31].

Integer getMonth()

getMonth() returns the month as an Integer [1-12].

Integer getWeek()

getWeek() returns the number of the week as an Integer [1-53].

Integer getWeekDay()

getWeekDay() returns the day of the week as an Integer [0-6].
The 1st day of the week is Monday and has index 0!

Integer getYear()

getYear() returns the year as an Integer.

Time getTime()

getTime() returns the time-portion as a Time object.

Date getDate()

getDate() returns the date part of the DateTime

Comparing timestamps

Integer diff(DateTime otherDateTime)

diff() returns the difference in the number of seconds between 2 timestamps. The method subtracts the passed timestamp from the DateTime object you invoke diff() on.
This will print -3600 because dt2 is 1 hour (60x60 seconds) ahead of dt1. Understanding return values: dt1 < dt2 means that dt1 comes before dt2 in chronological time.

UNIX time

For when you need to convert between SuperOffice and integrated systems running on UNIX time.

DateTime setUnix(Integer number)

setUnix() sets the date and time to the number of the seconds since 01.01.1970 00:00:00.

Integer getUnix()

getUnix() returns the number of seconds since 01.01.1970 00:00:00.

No value

Before a DateTime is initialized, it has no value. This is commonly written as NULL, NUL, or NIL in other programming languages. CRMScript automatically initializes DateTime objects when declared to the current timestamp. Thus this situation is uncommon. However, it is a good habit to always test that you have a value before using it.

Bool isNull()

isNull() will return true if it has no value and false if it does.

Formatting options

Week numbers and year

ISOWY2/4 will calculate and return the year corresponding to the ISO week number of the given date. This year may be different from the “date” year.

Month and day

WEEKDAY differs from the getWeekDay() method of the Date object where Monday has index 0! WDAY and MONTH do start at index 0, thus construct your lists so that the indexes line up.

Time

AMPM returns either am or pm.

Modes

Remarks:
  • 12 is HTTP-date
  • 13 is SOAP standard formatting
toString() will not adjust to GMT, so you will have to do it yourself!