Let’s talk formatting the date. In our expression we had the following piece of code.
var presented = new Date(datetime);
This creates a new Date object called presented. We can use this object to generate the different formatting options.
For example…
var fullyear = ” + presented.getFullYear();
This creates a new variable called fullyear. We need to first say it equals two single quotation marks at the start with nothing in between to tell our expression to start to treat this variable as a string. (Don’t know about variables and strings, watch my tutorial here) Then using + we can then add the result of a date function used on our presented variable to get a particular value.
The final line is where we define what value is actually set is. Value is a special variable in expressions as it represents the final value that the property is set with. So regardless of any code you write, if you define what value is, that is what that value will be unless you redefine it later in your expression.
value = presented.getDate() + ‘ ‘ + months[presented.getMonth()] + ” ‘” + fullyear.substr(2,2);
We have a few values getting added one after the other for our final value. First we are using the .getDate() function to return the day of the month. Secondly we use presented.getMonth() to get the current month, where 0 is the first month. Finally we use fullyear.substr(2,2) to select two digits, two digits in from the start of the value of fullyear.
If we want to customise our date formatting to be different we can use any of the following to create variables prior to setting what value equals, and then use + to add them on to our value result. Adding + ‘ ‘ + will also add a space between two values.
- var dayofmonth = ” + presented.getDate();
returns the day of the month, e.g. 24
- var fullyear = ” + presented.getFullYear();
returns the full year, e.g. 2022
- var month = ” + presented.getMonth();
returns the current month where 0 is January, e.g. 3
- var dayofweek = ” + presented.getDay();
returns the current month where 0 is Sunday through to 6 for Saturday
For dayofmonth and dayofweek it is easiest to use these to select from an array of options.
For Months use….
var months = [“Jan”, “Feb”, “Mar”, “Apr”, “May”, “Jun”, “Jul”, “Aug”, “Sep”, “Oct”, “Nov”, “Dec”];
For days of the week use….
var daysweek = [“Sun”, “Mon”, “Tue”, “Wed”, “Thu”, “Fri”, “Sat”];
Then refer to them simply as…..
value = months[dayofmonth];
or
value = daysweek[dayofweek];
You can also reword each month or week day value to whatever format suits your needs.