Autosync has quite powerful features for date processing and output. Every Date instance will be processed in relevance with the default format (YYYY-MM-DD HH:mm:ss
) before output into the template. You can also provide custom format using the date
filter:
<span class="email" asmodel="user.profile_data">
Registration date: {$value['registered_at']|date('MM/D/YYYY')}">
</span>
Autosync adds a special now
object to the window (it is a function in compat version, and a computed property in the basic one). This method is a wrapper on the current date object and contains the following properties:
raw | Date class object | ||||||||||||||||
timestamp | UNIX timestamp with milliseconds (integer Date object representation) | ||||||||||||||||
date | String in the YYYY-MM-DD format |
||||||||||||||||
datetime | String in the YYYY-MM-DD HH:mm:ss format |
||||||||||||||||
format(s) | Function formatting date as defined by format string s
Possible formats list:
|
Pay attention to the fact that these properties are plain functions in compat version, so you have to access them with parentheses there.
Luckily, inside templates and attributes you don't have to figure out these properties' type or the browser version by yourself. Autosync supports unified syntax $now.field
or, in case you need to call a method, $now.method(args).field
, which will compile this to the JS code that is needed at the current moment.
Also now
object has the following methods:
minusHour(n) | Minus n hours or minus 1 hour if n is not defined |
plusHour(n) | Plus n hours or plus 1 hour if n is not defined |
minusDay(n) | Minus n days or minus 1 day if n is not defined |
plusDay(n) | Plus n days or plus 1 day if n is not defined |
minusWeek(n) | Minus n weeks or minus 1 week if n is not defined |
plusWeek(n) | Plus n weeks or plus 1 week if n is not defined |
minusMonth(n) | Minus n months or minus 1 month if n is not defined |
plusMonth(n) | Plus n months or plus 1 month if n is not defined |
minusYear(n) | Minus n years or minus 1 year if n is not defined |
plusYear(n) | Plus n years or plus 1 year if n is not defined |
These methods return the current object with modified state, so you can chain them.
You can use these methods and properties in the following contexts:
- In templates
- In
asif
attribute - In
asclasses
andasstyles
attributes - In
as-on*
attributes
For example, you can use the following condition to hide the <select> element changing the assigned user:
<select asif="$value['completed_at'] < $now.date" asmodel="data.users" aslist>
<option value="{$value['id']}">{$value['name']}</option>
</select>