Provides a java.util.Date object wrapper. An equivalent subclass Ax.sql.Date exists having same properties plus specific database properties.

You can use:

  • Ax.util.Date for regular timestamp operations where no database is involved.
  • Ax.sql.Date for date operations where database is involved.

1 Introduction

The date object can be used to perform date computation as well as database operations.

Copy
// Date(year, month, day)
var d1 = new Ax.util.Date(2018, 12, 31);
// Date(year, month, day, hour, minute, second)
var d2 = new Ax.util.Date(2018, 12, 31, 10, 33, 30);
// create a data from millis
var d3 = new Ax.util.Date(d1.getTime());
// current
var d4 = new Ax.util.Date();
// current (javascript)
var d5 = new Date();

console.log("d1 value    = " + d1);
console.log("d2 value    = " + d2);
console.log("d3 value    = " + d3);
console.log("d4 value    = " + d4);
console.log("d5 value    = " + d5);
console.log("d1 class    = " + d1.class);
console.log("d2 class    = " + d2.class);
console.log("d5 class    = " + d5.class);

console.log("" + d2.toISOString());
console.log("" + d2.toSQLQueryString());

var d2b = d2.clearTime();
console.log("d2b value    = " + d2b);
d1 value    = Sun Mar 03 00:00:00 CET 2019
d2 value    = Sun Mar 03 10:33:30 CET 2019
d3 value    = Sun Mar 03 00:00:00 CET 2019
d4 value    = Thu Feb 07 12:53:45 CET 2019
d5 value    = Thu Feb 07 2019 12:53:45 GMT+0100 (CET)
d1 class    = class deister.axional.server.dbstudio.script.js.Ax.util.JSDate
d2 class    = class deister.axional.server.dbstudio.script.js.Ax.util.JSDate
d5 class    = undefined

2 Adding quantums to date

You can add quantum units to a date referring to any of it's elements from years to milliseconds.

Return Function Description
Ax.util.Date addYear(int ammount) Add the number of years specified to date object returning a new one
Ax.util.Date addMonth(int ammount) Add the number of months specified to date object returning a new one
Ax.util.Date addDay(int ammount) Add the number of days specified to date object returning a new one
Ax.util.Date addHour(int ammount) Add the number of hours specified to date object returning a new one
Ax.util.Date addMinute(int ammount) Add the number of minutes specified to date object returning a new one
Ax.util.Date addSecond(int ammount) Add the number of seconds specified to date object returning a new one
Ax.util.Date addMillisecond(int ammount) Add the number of milliseconds specified to date object returning a new one
Ax.util.Date set(int field, int ammount) Sets the date field to given value

The value constants for field are available on Ax.util.Calendar static constants named:

  • Ax.util.Calendar.YEAR
  • Ax.util.Calendar.MONTH
  • Ax.util.Calendar.DAY_OF_YEAR
  • Ax.util.Calendar.DAY_OF_MONTH
  • Ax.util.Calendar.DAY_OF_WEEK
  • Ax.util.Calendar.DAY_OF_WEEK_IN_MONTH
  • Ax.util.Calendar.HOUR
  • Ax.util.Calendar.MINUTE
  • Ax.util.Calendar.SECOND
  • Ax.util.Calendar.MILLISECOND
  • Ax.util.Calendar.HOUR_OF_DAY

Copy
<script>
    var d = new Ax.util.Date(2018, 12, 31, 10, 33, 30);
    console.log("d value    = " + d);

    d = d.addYear(1)
         .addMonth(-1);
    console.log("d +1Y -1M  = " + d);
</script>
d value    = Sun Mar 03 10:33:30 CET 2019
d +1Y -1M  = Mon Feb 03 10:33:30 CET 2020

Either add and set operations return a new Date object, leaving original object immutable.

3 Date getters

int getYear The year of the date
int getMonth The month of the date [0-11]
int getDate The day of the month of the date [1-31]
int getDay The day of the week of the date [0-6]
int getHours The hours of the date
int getMinutes The minutes of the date
int getSeconds The seconds of the date
Copy
var d1 = new Ax.util.Date(2019, 1, 6);

let year  = d1.getFullYear();
let month = d1.getMonth();
let day   = d1.getDate();
let dayOfWeek   = d1.getDay();

let hours     = d1.getHours();
let minutes   = d1.getMinutes();
let seconds   = d1.getSeconds();


console.log(year, month, day, hours, minutes, seconds)
[2019,0,6,0,0,0]

4 Date setters

Date setters alters date object contents and does not return the object or an object clone.

setYear(int) Set year of the date
setMonth(int) Set month of the date [0-11]
setDate(int) Set day of the date
setHours(int) Set hours of the date
setMinutes(int) Set minutes of the date
setSeconds(int) Set seconds of the date
Copy
var m_today = new Ax.util.Date();
var m_monthday = m_today.getDate();;

if (m_today.getDate() == m_monthday) {
    let my_date = m_today.clone();  // Clone Date Object
    my_date.setDate(1);             // Set Day to first of current month
    my_date.setHours(0);            // get rid of time
    my_date.setMinutes(0);
    my_date.setSeconds(0);

    let m_prevmonth_first = my_date.addMonth(-1);
    let m_prevmonth_last  = my_date.addDay(-1);

    console.log(`Previous month first day: ${m_prevmonth_first} last day: ${m_prevmonth_last}`);
} else {
    console.log(`TODAY is Not Day ${m_monthday}`);
}
Previous month first day: Sat Jan 01 00:00:00 CET 2022 last day: Mon Jan 31 00:00:00 CET 2022

5 Difference between dates

The Ax.util.Date & Ax.sql.Date objects provides methods to perform calculation of time between the date and other date. The calculation returns a whole number, representing the number of complete units between the two temporals. For example, the amount in hours between the times 11:30 and 13:29 will only be one hour as it is one minute short of two hours.

Return Function Description
long millis(Ax.util.Date d1) Returns the difference between two dates in milliseconds
long seconds(Ax.util.Date d1) Returns the difference between two dates in seconds
long minutes(Ax.util.Date d1) Returns the difference between two dates in minutes
long hours(Ax.util.Date d1) Returns the difference between two dates in hours
long days(Ax.util.Date d1) Returns the difference between two dates in days
long weeks(Ax.util.Date d1) Returns the difference between two dates in weeks
long days(Ax.util.Date d1) Returns the difference between two dates in months
long days(Ax.util.Date d1) Returns the difference between two dates in years

5.1 Calculate days between two dates

Copy
<script>
    var date1 = new Ax.util.Date(2019, 12, 30);
    var date2 = new Ax.util.Date(2019, 12, 31);
    console.log("Days between: " + date1 + " " + date2 + " " + date1.days(date2));
    Ax.jsunit.assertEquals(1, date1.days(date2));
    
    var date1 = new Ax.util.Date(2019, 12, 30);
    var date2 = new Ax.util.Date(2020, 1,   1);
    console.log("Days between: " + date1 + " " + date2 + " " + date1.days(date2));
    Ax.jsunit.assertEquals(2, date1.days(date2));
</script>
1
2

5.2 Calculate all intervals between two dates

Copy
var date1 = new Ax.util.Date(2019, 12, 30);
var date2 = new Ax.util.Date(2021, 1,   1);
console.log("   Time between: " + date1);
console.log("            and: " + date2);
console.log("---------------: ");
console.log(" millis between: " + date1.millis(date2));
console.log("seconds between: " + date1.seconds(date2));
console.log("minutes between: " + date1.minutes(date2));
console.log("  hours between: " + date1.hours(date2));
console.log("   days between: " + date1.days(date2));
console.log("  weeks between: " + date1.weeks(date2));
console.log(" months between: " + date1.months(date2));
console.log("  years between: " + date1.years(date2));
.
   Time between: Mon Dec 30 00:00:00 CET 2019
            and: Fri Jan 01 00:00:00 CET 2021
---------------: 
 millis between: 31795200000
seconds between: 529920
minutes between: 529920
  hours between: 8832
   days between: 368
  weeks between: 52
 months between: 12
  years between: 1

5.3 Date comparison

Comparing dates in JavaScript always use the String representation of the Date object. As this cannot be sorted alphabetically this will not work.

Object comparison methods always include time component in object. So trying to compare two DATEs without time component will not work

You need to use "difference methods" to compute number of days or months or years to compare two dates without time component.

Copy
// Date(year, month, day, hour, minute, second)
var d1 = new Ax.util.Date(2010, 11, 19, 00, 00, 00);
// Date(year, month, day, hour, minute, second)
var d2 = new Ax.util.Date(2010, 11, 19, 10, 33, 30);


console.log("d1 = " + d1.toString());
console.log("d2 = " + d2.toString());
// Incorrect comparissions as dates are converted to Strings before comparing
console.log("d1 < d2  = " + (d1 < d2));
console.log("d1 > d2  = " + (d1 > d2));

// Compare dates including time component
console.log("d1 <-> d2  = " + d1.compareTo(d2));
console.log("d1 < d2    = " + (d1.before(d2)));
console.log("d1 > d2    = " + (d1.after(d2)));
console.log("d1 <= d2   = " + (d1.beforeOrEqual(d2)));
console.log("d1 >= d2   = " + (d1.afterOrEqual(d2)));
// To compare two dates without time component
console.log("Days between d1 and d2 = " + d1.days(d2));
d1 = Fri Nov 19 00:00:00 CET 2010
d2 = Fri Nov 19 10:33:30 CET 2010
d1 < d2  = true
d1 > d2  = false
d1 <-> d2  = -1
d1 < d2    = true
d1 > d2    = false
d1 <= d2   = true
d1 >= d2   = false
Days between d1 and d2 = 0

6 Date format

You can use the format method of a date object to format a date using SimpleDateFormat options.

Copy
<script>
    var current = new Ax.util.Date();
    console.log(current.format("yyyy.MM.dd G 'at' HH:mm:ss z"));
    // Default locale
    console.log(current.format("EEE, MMM d, ''yy"));
    // Locale in Spanish
    console.log(current.format("EEE, MMM d, ''yy", "es"));
    // Locale in French
    console.log(current.format("EEE, MMM d, ''yy", "fr"));
    console.log(current.format("h:mm a"));
    console.log(current.format("hh 'o''clock' a, zzzz"));
    console.log(current.format("K:mm a, z"));
    console.log(current.format("EEE, d MMM yyyy HH:mm:ss Z"));
    console.log(current.format("yyMMddHHmmssZ"));
    console.log(current.format("yyyy-MM-dd'T'HH:mm:ss.SSSZ"));
    console.log(current.format("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"));
    console.log(current.format("YYYY-'W'ww-u"));
</script>
2019.02.11 AD at 17:08:45 CET
Mon, Feb 11, '19
lun., feb. 11, '19
lun., févr. 11, '19
5:08 PM
05 o'clock PM, Central European Standard Time
5:08 PM, CET
Mon, 11 Feb 2019 17:08:45 +0100
190211170845+0100
2019-02-11T17:08:45.847+0100
2019-02-11T17:08:45.847+01:00
2019-W07-1

7 Date parse

The same way, you can use the static parse method to parse a date string literal into a date object.

Copy
<script>
    console.log(Ax.util.Date.parse("EEE, MMM d, ''yy", "Mon, Feb 11, '19"));
    console.log(Ax.util.Date.parse("EEE, MMM d, ''yy", "lun., févr. 11, '19", "fr"));
</script>
Mon Feb 11 00:00:00 CET 2019
Mon Feb 11 00:00:00 CET 2019

8 Using timezone

Be aware that java.util.Date objects do not contain any timezone information by themselves - you cannot set the timezone on a Date object. The only thing that a Date object contains is a number of milliseconds since the "epoch" - 1 January 1970, 00:00:00 UTC

So when creating a date you have an instant of time stored as a reference to UTC. If your local computer is at GMT+2, 10 AM (GMT+2) is stored as 8AM (UTC).

8.1 Create a date on a timezone

You may want to create a date using it's reference from a known timezone. For example, we can setup a data at 9AM on Europe/Paris timezone using a computer running in Maldives.

And even correct a data from a given timezone to another

Copy
<script>

    // Create a date located in Paris timezone
    var dt_par = new Ax.util.Date("2019-08-09 09:00:00 CET");
    // Same but creating a date in local then moving to paris timezone
    var dt_bcn = new Ax.util.Date(2019, 08, 09, 09, 00, 00, "Europe/Paris");
    // Move date from Paris to London -> 9AM in London is 8AM in Paris
    var dt_lon = new Ax.util.Date(2019, 08, 09, 09, 00, 00, "Europe/Paris").setTimeZone("Europe/London");
    console.log("dt_bcn:" + dt_bcn);
    console.log("dt_lon:" + dt_lon);

</script>
dt_par:Fri Aug 09 12:00:00 MVT 2019 -> 9AM in Paris
dt_bcn:Fri Aug 09 12:00:00 MVT 2019 -> 9AM in Paris
dt_lon:Fri Aug 09 11:00:00 MVT 2019 -> 8AM in Paris

As you can see we have two date objects created on Europe/Paris and one moved to London. The date, displayed from Maldives timezone (MVT) will shown at GMT+3.