Class DateTime
DateTime is a complex data type representing a timestamp with both date and time elements on ISO format YYYY-MM-DD. The default value is now.
Before a Date is initialized, it has no value. This is commonly written as NULL, NUL, or NIL in other programming languages. CRMScript automatically initializes Date objects when declared to the current date. Thus this situation is uncommon.Syntax
Examples
DateTime dt;
print(dt.toString());
Constructors
DateTime()
Default constructor.
Declaration
DateTime DateTime()
Returns
Type | Description |
---|---|
DateTime |
Examples
DateTime dt;
print(dt.toString());
DateTime(DateTime)
Pass a value to copy into a new object.
Declaration
DateTime DateTime(DateTime value)
Parameters
Type | Name | Description |
---|---|---|
DateTime | value | DateTime object. |
Returns
Type | Description |
---|---|
DateTime |
Examples
Date d;
Date next = Date(d);
printLine(next.toString());
DateTime(Integer, Integer, Integer, Integer, Integer, Integer)
Specify all elements of a DateTime individually. It accepts 6 integer values, representing year, month (1-12), day-of-month (1-31), hour (0-23), minute (0-59), and second (0-59). The constructor automatically calculates the weekday.
Declaration
DateTime DateTime(Integer year, Integer month, Integer day, Integer hour, Integer min, Integer sec)
Parameters
Type | Name | Description |
---|---|---|
Integer | year | The year portion of the timestamp. |
Integer | month | The month portion of the timestamp. |
Integer | mday | The day of month portion of the timestamp. |
Integer | hour | The hour portion of the timestamp. |
Integer | min | The minutes portion of the timestamp. |
Integer | sec | The seconds portion of the timestamp. |
Returns
Type | Description |
---|---|
DateTime |
Examples
DateTime schoolEnds = DateTime(2020,06,22,11,0,0);
DateTime(String)
Pass a String containing date and time. The constructor will parse the text and create a DateTime object. Formats:
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:XXXDeclaration
DateTime DateTime(String value)
Parameters
Type | Name | Description |
---|---|---|
String | value | A String containing a date (YYYY-MM-DD). |
Returns
Type | Description |
---|---|
DateTime |
Examples
DateTime graduation = DateTime("2020-06-22 11:00");
Methods
addDay(Integer)
Adjusts the currently set date with the given number of days.
Declaration
DateTime addDay(Integer num)
Parameters
Type | Name | Description |
---|---|---|
Integer | num | Number of days to add. |
Returns
Type | Description |
---|---|
DateTime |
Examples
DateTime dt;
dt.addDay(3);
addHour(Integer)
Adjusts the currently set date with the given number of hours.
Declaration
DateTime addHour(Integer num)
Parameters
Type | Name | Description |
---|---|---|
Integer | num | Number of hours to add. |
Returns
Type | Description |
---|---|
DateTime |
Examples
DateTime dt;
dt.addHour(3);
addMin(Integer)
Adjusts the currently set date with the given number of minutes.
Declaration
DateTime addMin(Integer num)
Parameters
Type | Name | Description |
---|---|---|
Integer | num | Number of minutes to add. |
Returns
Type | Description |
---|---|
DateTime |
Examples
DateTime dt;
dt.addMin(30);
addMonth(Integer)
Adjusts the currently set date with the given number of months.
Declaration
DateTime addMonth(Integer num)
Parameters
Type | Name | Description |
---|---|---|
Integer | num | Number of months to add. |
Returns
Type | Description |
---|---|
DateTime |
Remarks
The day remains unchanged regardless of the number of days in the months added or subtracted. However, if the update would result in February 29th in a year that is not a leap year, CRMScript automatically corrects it to March 1st.
Examples
DateTime dt;
dt.addMonth(3);
addSec(Integer)
Adjusts the currently set date with the given number of seconds.
Declaration
DateTime addSec(Integer num)
Parameters
Type | Name | Description |
---|---|---|
Integer | num | Number of seconds to add. |
Returns
Type | Description |
---|---|
DateTime |
Examples
DateTime dt;
dt.addSec(90);
addYear(Integer)
Adjusts the currently set date with the given number of years.
Declaration
DateTime addYear(Integer num)
Parameters
Type | Name | Description |
---|---|---|
Integer | num | Number of years to add. |
Returns
Type | Description |
---|---|
DateTime |
Examples
DateTime dt;
dt.addYear(1);
diff(DateTime)
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.
Declaration
Integer diff(DateTime value)
Parameters
Type | Name | Description |
---|---|---|
DateTime | value | The timestamp to subtract from the DateTime object you invoked diff() on. |
Returns
Type | Description |
---|---|
Integer | The number is negative if the input DateTime is the greatest. |
Examples
DateTime dt1;
DateTime dt2;
dt2.addHour(1);
print(dt1.diff(dt2).toString());
getDate()
Returns the date part of the DateTime
Declaration
Date getDate()
Returns
Type | Description |
---|---|
Date |
Examples
DateTime dt;
Date d = dt.getDate();
print(d.toString());
getMDay()
Returns the day of the month as an Integer [1-31].
Declaration
Integer getMDay()
Returns
Type | Description |
---|---|
Integer |
Examples
DateTime dt;
print(dt.getMDay().toString());
getMonth()
Returns the month as an Integer [1-12].
Declaration
Integer getMonth()
Returns
Type | Description |
---|---|
Integer |
Examples
DateTime dt;
print(dt.getMonth().toString());
getTime()
Returns the time-portion as a Time object.
Declaration
Time getTime()
Returns
Type | Description |
---|---|
Time |
Examples
DateTime dt;
Time t = dt.getTime();
print(t.toString());
getUnix()
Returns the date and time to the number of the seconds since 01.01.1970 00:00:00.
Declaration
Integer getUnix()
Returns
Type | Description |
---|---|
Integer |
Examples
DateTime dt;
print(dt.toString());
getWeek()
Returns the number of the week as an Integer [1-53].
Declaration
Integer getWeek()
Returns
Type | Description |
---|---|
Integer |
Remarks
See http://en.wikipedia.org/wiki/ISO_8601 for detailed info on ISO week numbers.
Examples
DateTime dt;
print(dt.getWeek().toString());
getWeekDay()
Returns the day of the week as an Integer [0-6].
Declaration
Integer getWeekDay()
Returns
Type | Description |
---|---|
Integer |
Remarks
The 1st day of the week is Monday and has index 0!
Examples
DateTime dt;
print(dt.getWeekDay().toString());
getYear()
Returns the year as an Integer.
Declaration
Integer getYear()
Returns
Type | Description |
---|---|
Integer |
Examples
DateTime dt;
print(dt.getYear().toString());
isNull()
Returns true if it has no value and false if it does.
Declaration
Bool isNull()
Returns
Type | Description |
---|---|
Bool |
Remarks
A NULL/NUL/NIL DateTime is different from zero, in that it is conceptually without a value.
Examples
DateTime dt;
print(dt.isNull().toString());
moveToDayEnd()
Moves the current DateTime to the end of the day (23:59:59). Returns a reference to itself.
Declaration
DateTime moveToDayEnd()
Returns
Type | Description |
---|---|
DateTime |
Examples
DateTime dt;
print(dt.toString());
moveToDayStart()
Moves the current DateTime to the start of the day, (00:00 o'clock). Returns a reference to itself.
Declaration
DateTime moveToDayStart()
Returns
Type | Description |
---|---|
DateTime |
Examples
DateTime dt;
print(dt.toString());
moveToHourEnd()
Moves the current DateTime to the end of the current hour. Returns a reference to itself.
Declaration
DateTime moveToHourEnd()
Returns
Type | Description |
---|---|
DateTime |
Examples
DateTime dt;
print(dt.toString());
moveToHourStart()
Moves the current DateTime to the start of the current hour. Returns a reference to itself.
Declaration
DateTime moveToHourStart()
Returns
Type | Description |
---|---|
DateTime |
Examples
DateTime dt;
print(dt.toString());
moveToMonthEnd()
Moves the current DateTime to the last second in the current month. Returns a reference to itself.
Declaration
DateTime moveToMonthEnd()
Returns
Type | Description |
---|---|
DateTime |
Examples
DateTime dt;
print(dt.toString());
moveToMonthStart()
Moves the current DateTime to the first second in the current month. Returns a reference to itself.
Declaration
DateTime moveToMonthStart()
Returns
Type | Description |
---|---|
DateTime |
Examples
DateTime dt;
print(dt.toString());
moveToQuarterEnd()
Moves the current DateTime to the end of current quarter: 31. of Mars, 30. of June, 30 of September or 31. of December. Time is set to 23:59:59. Returns a reference to itself.
Declaration
DateTime moveToQuarterEnd()
Returns
Type | Description |
---|---|
DateTime |
Examples
DateTime dt;
print(dt.toString());
moveToQuarterStart()
Moves the current DateTime to the start of the current quarter (1. of January, 1. of April, 1. of July, or 1. of October). Returns a reference to itself.
Declaration
DateTime moveToQuarterStart()
Returns
Type | Description |
---|---|
DateTime |
Examples
DateTime dt;
print(dt.toString());
moveToWeekEnd()
Moves the current DateTime forward to 23:59:59 on Sunday of the current week. Returns a reference to itself.
Declaration
DateTime moveToWeekEnd()
Returns
Type | Description |
---|---|
DateTime |
Examples
DateTime dt;
print(dt.toString());
moveToWeekStart()
Moves the current DateTime back to 00:00:00 on monday in the current week. Returns a reference to itself.
Declaration
DateTime moveToWeekStart()
Returns
Type | Description |
---|---|
DateTime |
Examples
DateTime dt;
print(dt.toString());
moveToYearEnd()
Moves the current DateTime to the last second in the current year. Returns a reference to itself.
Declaration
DateTime moveToYearEnd()
Returns
Type | Description |
---|---|
DateTime |
Examples
DateTime dt;
print(dt.toString());
moveToYearStart()
Moves the current DateTime to the first second in the current year. Returns a reference to itself.
Declaration
DateTime moveToYearStart()
Returns
Type | Description |
---|---|
DateTime |
Examples
DateTime dt;
print(dt.toString());
setTime(Time)
Sets the time-part of a DateTime by passing a Time object.
Declaration
Void setTime(Time theTime)
Parameters
Type | Name | Description |
---|---|---|
Time | theTime | The time to set. |
Returns
Type | Description |
---|---|
DateTime |
Examples
Time t;
DateTime dt;
dt.setTime(t);
setUnix(Integer)
Sets the date and time to the number of the seconds since 01.01.1970 00:00:00.
Declaration
DateTime setUnix(Integer number)
Parameters
Type | Name | Description |
---|---|---|
Integer | number | The number of the seconds since 01.01.1970 00:00:00. |
Returns
Type | Description |
---|---|
DateTime |
Examples
DateTime dt;
print(dt.toString());
toString()
Converts a DateTime value to its string representation.
One of the most frequently used methods, typically when you are going to output something.Declaration
String toString()
Returns
Type | Description |
---|---|
String |
Remarks
If you do not want the name of the months and the days, use the toString-function with only one parameter, toString(String format)
Examples
DateTime dt;
printLine(dt.toString());
toString(Integer, Integer, Bool)
A variant of toString() that takes codes for mode and language as Integers and a boolean indicator for 12- or 24-hour clock.
Declaration
String toString(Integer mode, Integer language, Bool 24HourMode)
Parameters
Type | Name | Description |
---|---|---|
Integer | mode | 0-16. |
Integer | language | 0 = Norwegian; 1 = English; 2 = German; 3 = Swedish; 4 = Danish; 5 = Dutch |
Bool | 24HourMode | Indicator for 12- or 24-hour clock. True = 24h, false = 12h. |
Returns
Type | Description |
---|---|
String |
Remarks
Modes:
Code | Name | Format | Example |
---|---|---|---|
0 | modeNewDate | YYYY-MM-DD | 2020-05-29 |
1 | modeNew2Min | YYYY-MM-DD hh:mm | 2020-05-29 13:37 |
2 | modeNew2Sec | YYYY-MM-DD hh:mm:ss | 2020-05-29 13:37:42 |
3 | modeTextDate | DD. MMM YYYY (no) | May 29. 2020 |
MMM DD. YYYY (en) | 29. Mai 2020 | ||
4 | modeText2Min | DD. MMM YYYY 11:23 | May 29. 2020 13:37 |
5 | modeText2Sec | DD. MMM YYYY 11:23:15 | May 29. 2020 13:37:42 |
6 | modeText2MinLong | DD. MMM YYYY hh:mm (no) | 29. Mai 2020 13:37 |
MMM. DD. YYYY hh:mm (en) | May 29. 2020 13:37 | ||
7 | modeShort2Min | MM/DD/YYYY hh:mm (no) | 29/05/2020 13:37 |
DD/MM/YYYY hh:mm (en) | 05/29/2020 13:37 | ||
8 | modeNumeric | YYYYMMDDhhmmss | 20200529133742 |
9 | modeTime2Min | hh:mm | 13:37 |
10 | modeTime2Sec | hh:mm:ss | 13:37:42 |
11 | modeCompressed | YYYYMMDDhhmmss | 20200529133942 |
12 | modeRFC1123 | ddd, DD MMM YY hh:mm:ss GMT | Fri, 29 May 20 13:37:42 GMT |
13 | modeSoap | YYYY-DD-MMThh:mm:ss | 2020-05-29T13:37:42 |
14 | modeRFC822 | ddd, DD MMM YYYY hh:mm:ss +hhmm | Fri, 29 May 2020 13:37:42 +0200 |
15 | modeDateFirst | MM.DD.YYYY hh.mm | 29.05.2020 13:37 |
16 | modeSlash2Min | MM/DD/YYYY hh.mm | 29/05/2020 13:37 |
Examples
DateTime dt;
printLine(dt.toString(6,1,true));
toString(String)
A variant of toString() that takes a string with one or more formatting codes. You can also include white-space and punctuation marks.
Declaration
String toString(String format)
Parameters
Type | Name | Description |
---|---|---|
String | format | A code for how to format the output. |
Returns
Type | Description |
---|---|
String |
Remarks
Available codes: ISOW1, ISOW2, ISOWY2, ISOWY4, YY2, YY4, MM1, MM2, DD1, DD2, WEEKDAY, MONTH, WDAY, H24, HH24, H12, HH12, MI2, SS2. For details, see the CRMScript docs.
Examples
DateTime dt;
printLine(dt.toString("HH12:MI2 AMPM"));
toString(String, String, String)
A variant of toString() that takes a string with one or more formatting codes. You can also include white-space and punctuation marks.
Declaration
String toString(String format, String months, String weekDays)
Parameters
Type | Name | Description |
---|---|---|
String | format | A code for how to format the output. |
String | months | A code for how to format the output. |
String | weekDays | A code for how to format the output. |
Returns
Type | Description |
---|---|
String |
Remarks
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.
Available codes: ISOW1, ISOW2, ISOWY2, ISOWY4, YY2, YY4, MM1, MM2, DD1, DD2, WEEKDAY, MONTH, WDAY, H24, HH24, H12, HH12, MI2, SS2. For details, see the CRMScript docs.Examples
DateTime dt;
String days="søndag,mandag,tirsdag,onsdag,torsdag,fredag,lørdag";
printLine(dt.toString("WDAY uke ISOW1","",days));