1 Date creation
There are many ways to create dates in XSQL and server-side scripts. To get the current date, the definition is:
<xsql-script> <body> <println> <date.current /> </println> </body> </xsql-script>
21-12-2020 15:58:15
var mDateFecmov = new Ax.util.Date(); console.log(mDateFecmov);
Tue, 21 Dec 2020 15:59:23 CET
It is possible to get a specific date by defining it even down to the millisecond level. The levels that can be reported are:
- Year (required)
- Month (required)
- Day (required)
- Hour
- Minute
- Second
- Milisecond
<xsql-script name='date_sample'> <body> <println><date dd='22' MM='12' yy='2020' /></println> </body> </xsql-script>
22-12-2020
var mDateFecmov = new Ax.util.Date(2020,12,22); console.log(mDateFecmov);
Sun, 22 Nov 2020 00:00:00 CET
<xsql-script> <body> <println><date dd='22' MM='12' yy='2020' hh='15' mm='22' ss='08' millisecond='12' /></println> </body> </xsql-script>
22-12-2020
var mDateFecmov = new Ax.util.Date(2020,12,22,15,22,08,12); console.log(mDateFecmov);
Sun, 22 Nov 2020 15:22:08 CET
It is also possible create a date from a string
<xsql-script> <body> <println><date format='dd-MM-yyyy'>22-12-2020</date></println> </body> </xsql-script>
22-12-2020
var mStrDate = '2020-12-22' var mDateFecmov = new Ax.util.Date(mStrDate); console.log(mDateFecmov);
Tue, 22 Dec 2020 00:00:00 CET
2 Date getters
There are methods in both languages that allow to obtain individually information of a date, like the year, the month, the day, etc.
Operator XSQL | Equivalent server-side script | Description |
---|---|---|
<date.year> | getFullYear() | The year of the date |
<date.month> | getMonth() | The month of the date |
<date.day> | getDate() | The day of the month of the date |
<date.dayOfWeek> | getDay() | The day of the week of the date |
<date.hour> | getHours() | The hours of the date |
<date.minute> | getMinutes() | The minutes of the date |
<date.second> | getSeconds() | The seconds of the date |
2.1 Getting the year
With this method obtain the year as a four digit number (yyyy)
<xsql-script> <body> <set name='m_date'> <date format='dd-MM-yyyy HH:mm:ss'>22-12-2020 16:48:55 GMT</date> </set> <println><date.year><m_date /></date.year></println> </body> </xsql-script>
2020
var mDateFecha = new Ax.util.Date(2020,12,22,16,48,55); var mIntResult = mDateFecha.getFullYear(); console.log(mIntResult);
2020
2.2 Getting the month
Get the month of the indicated date as a number
In XSQL script, the first month (January) is month number 1, and December is month number 12
In server-side javascript, the first month (January) is month number 0, so December returns month number 11.
<xsql-script> <body> <set name='m_date'> <date format='dd-MM-yyyy HH:mm:ss'>22-12-2020 16:48:55 GMT</date> </set> <println><date.month><m_date /></date.month></println> </body> </xsql-script>
12
var mDateFecha = new Ax.util.Date(2020,12,22,16,48,55); var mIntResult = mDateFecha.getMonth(); console.log(mIntResult);
11
In this example, the month is December and the method returns month number 11.
2.3 Getting the day
Get the day of the indicated date as a number (1-31)
<xsql-script> <body> <set name='m_date'> <date format='dd-MM-yyyy HH:mm:ss'>22-12-2020 16:48:55 GMT</date> </set> <println><date.day><m_date /></date.day></println> </body> </xsql-script>
22
var mDateFecha = new Ax.util.Date(2020,12,22,16,48,55); var mIntResult = mDateFecha.getDate(); console.log(mIntResult);
22
2.4 Getting the day of the week
Get the day of the week of the indicated date as a number (0-6)
<xsql-script name='date_day_week'> <body> <set name='m_date'> <date format='dd-MM-yyyy HH:mm:ss'>22-12-2020 16:48:55 GMT</date> </set> <println><date.dayOfWeek><m_date /></date.dayOfWeek></println> </body> </xsql-script>
2
var mDateFecha = new Ax.util.Date(2020,12,22,16,48,55); var mIntResult = mDateFecha.getDay(); console.log(mIntResult);
2
2.5 Getting the hour
Get the hour of the indicated date as a number (0-23)
<xsql-script name='date_hours'> <body> <set name='m_date'> <date format='dd-MM-yyyy HH:mm:ss'>22-12-2020 16:48:55 GMT</date> </set> <println><date.hour><m_date /></date.hour></println> </body> </xsql-script>
16
var mDateFecha = new Ax.util.Date(2020,12,22,16,48,55); var mIntResult = mDateFecha.getHours(); console.log(mIntResult);
16
2.6 Getting the minute
Get the minute of the indicated date as a number (0-59)
<xsql-script name='date_minutes'> <body> <set name='m_date'> <date format='dd-MM-yyyy HH:mm:ss'>22-12-2020 16:48:55 GMT</date> </set> <println><date.minute><m_date /></date.minute></println> </body> </xsql-script>
48
var mDateFecha = new Ax.util.Date(2020,12,22,16,48,55); var mIntResult = mDateFecha.getMinutes(); console.log(mIntResult);
48
2.7 Getting the seconds
Get the second of the indicated date as a number (0-59)
<xsql-script> <body> <set name='m_date'> <date format='dd-MM-yyyy HH:mm:ss'>22-12-2020 16:48:55 GMT</date> </set> <println><date.second><m_date /></date.second></println> </body> </xsql-script>
55
var mDateFecha = new Ax.util.Date(2020,12,22,16,48,55); var mIntResult = mDateFecha.getSeconds(); console.log(mIntResult);
55
3 Date adders
You can add quantum units to a date referring to any of it's elements from years to milliseconds.
Operator XSQL | Equivalent server-side script | Description |
---|---|---|
<date.units> | addYear() | Add specified years to a date |
<date.units> | addMonth() | Add specified months to a date |
<date.units> | addDay() | Add specified days to a date |
<date.units> | addHour() | Increase hours to a date |
<date.units> | addMinute() | Increase minutes to a date |
<date.units> | addSecond() | Increase seconds to a date |
3.1 Add the year
Add specified years to a date.
Operator XSQL | Equivalent server-side script | Description |
---|---|---|
<date.units> | addYear() | Add specified years to a date |
<xsql-script> <body> <set name='m_date' type='date'>28-12-2020</set> <set name='m_new_date'> <add> <m_date /><date.units type='y'>2</date.units> </add> </set> <println><m_new_date /></println> </body> </xsql-script>
28-12-2022
var mDateFecha = new Ax.util.Date(2020,12,28); mDateFecha = mDateFecha.addYear(2); console.log(mDateFecha);
Wed, 28 Dec 2022 00:00:00 CET
3.2 Add the month
Add specified months to a date.
Operator XSQL | Equivalent server-side script | Description |
---|---|---|
<date.units> | addMonth() | Add specified months to a date |
<xsql-script> <body> <set name='m_date' type='date'>28-12-2020</set> <set name='m_new_date'> <add> <m_date /><date.units type='m'>2</date.units> </add> </set> <println><m_new_date /></println> </body> </xsql-script>
28-02-2021
var mDateFecha = new Ax.util.Date(2020,12,28); mDateFecha = mDateFecha.addMonth(2); console.log(mDateFecha);
Sun, 28 Feb 2021 00:00:00 CET
3.3 Add the day
Add specified days to a date.
Operator XSQL | Equivalent server-side script | Description |
---|---|---|
<date.units> | addDay() | Add specified days to a date |
<xsql-script> <body> <set name='m_date' type='date'>28-12-2020</set> <set name='m_new_date'> <add> <m_date /><date.units type='d'>5</date.units> </add> </set> <println><m_new_date /></println> </body> </xsql-script>
02-01-2021
var mDateFecha = new Ax.util.Date(2020,12,28); mDateFecha = mDateFecha.addDay(5); console.log(mDateFecha);
Sat, 02 Jan 2021 00:00:00 CET
3.4 Add the hour
Increase hours to a date.
Operator XSQL | Equivalent server-side script | Description |
---|---|---|
<date.units> | addHour() | Increase hours to a date |
<xsql-script> <body> <set name='m_date' type='date'>28-12-2020 12:34:27</set> <set name='m_new_date'> <add> <m_date /><date.units type='HH'>10</date.units> </add> </set> <println><m_new_date /></println> </body> </xsql-script>
28-12-2020 10:00:00
var mDateHora = new Ax.util.Date(2020, 12, 28, 12, 33, 30); mDateHora = mDateHora.addHour(10); console.log(mDateHora);
Fri, 28 Dec 2020 22:33:30 CET
3.5 Add the minute
Increase minutes to a date.
Operator XSQL | Equivalent server-side script | Description |
---|---|---|
<date.units> | addMinute() | Increase minutes to a date |
<xsql-script> <body> <set name='m_date' type='date'>28-12-2020 13:23:27</set> <set name='m_new_date'> <add> <m_date /><date.units type='mm'>10</date.units> </add> </set> <println><m_new_date /></println> </body> </xsql-script>
28-12-2020 00:10:00
var mDateMinutos = new Ax.util.Date(2020, 12, 28, 13, 23, 27); mDateMinutos = mDateMinutos.addMinute(10); console.log(mDateMinutos);
Mon, 28 Dec 2020 13:33:27 CET
3.6 Add the seconds
Increase seconds to a date.
Operator XSQL | Equivalent server-side script | Description |
---|---|---|
<date.units> | addSecond() | Increase seconds to a date |
<xsql-script> <body> <set name='m_date' type='date'>28-12-2020 14:40:27</set> <set name='m_new_date'> <add> <m_date /><date.units type='ss'>10</date.units> </add> </set> <println><m_new_date /></println> </body> </xsql-script>
28-12-2020 00:00:10
var mDateSegundos = new Ax.util.Date(2020, 12, 28, 14, 40, 27); mDateSegundos = mDateSegundos.addSecond(10); console.log(mDateSegundos);
Mon, 28 Dec 2020 14:40:37 CET
4 Date comparison
It is possible to compare two dates using relational operators. In JavaScript is necessary to use the method getTime()
<xsql-script> <body> <set name='m_date1' type='date'>04-07-2020</set> <set name='m_date2' type='date'>06-10-2020</set> <ifthen> <expr> <gt><m_date1/><m_date2/></gt> </expr> <println>date1 is greater than date2</println> <println>date2 is greater than date1</println> </ifthen> </body> </xsql-script>
date2 is greater than date1
let mDate1 = new Ax.util.Date(2020,7,4); let mDate2 = new Ax.util.Date(2020,10,6); mDate1.getTime() > mDate2.getTime() ? console.log('date1 is greater than date2') : console.log('date2 is greater than date1');
date2 is greater than date1
5 Date as a prepared statement
It is possible to use a date as a prepared statement in database declarations.
<xsql-script> <body> <set name='m_date'> <date format='dd-MM-yyyy HH:mm:ss'>22-12-2020 16:48:55 GMT</date> </set> <print.sql> <select first='5'> <columns> tabname, tabid </columns> <from table='systables' /> <where> created = <m_date /> </where> </select> </print.sql> </body> </xsql-script>
+----------------------------------------+----------+
|tabname |tabid |
+----------------------------------------+----------+
|systables |1 |
|syscolumns |2 |
|sysindices |3 |
|systabauth |4 |
|syscolauth |5 |
+----------------------------------------+----------+
var mDateFecha = new Ax.util.Date(2020,12,22,16,48,55); var mRsSystables = Ax.db.executeQuery(` <select first='5'> <columns> tabname, tabid </columns> <from table='systables' /> <where> created = ? </where> </select> `, mDateFecha).toMemory(); console.log(mRsSystables);
+----------------------------------------+----------+
|systables |systables |
|tabname |tabid |
|varchar(128) |serial |
+----------------------------------------+----------+
|systables |1 |
|syscolumns |2 |
|sysindices |3 |
|systabauth |4 |
|syscolauth |5 |
+----------------------------------------+----------+
It is also possible to set a variable of the type date at the column level of a database query. While in XSQL script the same variable could do this, in server-side scripts the date variable is required to pass through the setConnection(Axdb) function and use template literals to embed the expression.
Template literals are enclosed by the back-tick (` `) (grave accent) character instead of double or single quotes and are indicated by the dollar sign and curly braces (${expression}).
<xsql-script> <body> <set name='m_date'> <date format='dd-MM-yyyy HH:mm:ss'>22-12-2020 16:48:55 GMT</date> </set> <print.sql> <select first='5'> <columns> tabname, tabid, <m_date /> date_test </columns> <from table='systables' /> <where> tabname = 'systables' </where> </select> </print.sql> </body> </xsql-script>
+----------------------------------------+----------+------------+
|tabname |tabid |date_test |
+----------------------------------------+----------+------------+
|systables |1 |Dec 22, 2020|
+----------------------------------------+----------+------------+
var mDateFecha = new Ax.util.Date(2020,12,22,16,48,55); mDateFecha = mDateFecha.setConnection(Ax.db); let mRsSystables = Ax.db.executeQuery(` <select first='5'> <columns> tabname, tabid, ${mDateFecha} date_test </columns> <from table='systables' /> <where> tabname = 'systables' </where> </select> `).toMemory(); console.log(mRsSystables);
+----------------------------------------+----------+------------+
|systables |systables |systables |
|tabname |tabid |date_test |
|varchar(128) |serial |date |
+----------------------------------------+----------+------------+
|systables |1 |Dec 22, 2020|
+----------------------------------------+----------+------------+