Date
Module to generate dates.
Overview
To quickly generate a date in the past, use recent() (last day) or past() (last year). To quickly generate a date in the future, use soon() (next day) or future() (next year). For a realistic birthdate for an adult, use birthdate().
For more control, any of these methods can be customized with further options, or use between() to generate a single date between two dates, or betweens() for multiple dates.
You can generate random localized month and weekday names using month()
and weekday()
.
anytime
Generates a random date that can be either in the past or in the future.
Parameters
Name | Type | Default | Description |
---|---|---|---|
refDate | string | number | Date | {{$date.now}} | The date to use as reference point for the newly generated date. |
Returns: Date
Examples
{{$date.anytime}} // '2024-12-16 08:23:11'
between
Generates a random date between the given boundaries.
Parameters
Name | Type | Default | Description |
---|---|---|---|
from | string | number | Date | The early date boundary. | |
to | string | number | Date | The late date boundary. |
Returns: Date
Examples
{{$date.between(from='1992-11-22',to='2024-11-22')}} // '2012-06-24 17:54:32'
betweens
Generates random dates between the given boundaries. The dates will be returned in an array sorted in chronological order.
Parameters
Name | Type | Default | Description |
---|---|---|---|
count | number | { min: number; max: number; } | 3 | The number of dates to generate. |
from | string | number | Date | The early date boundary. | |
to | string | number | Date | The late date boundary. |
Returns: Date
Examples
{{$date.between(from='1992-11-22',to='2024-11-22')}} // '2012-06-24 17:54:32'
{{$date.betweens(count=2,from='2020-01-01',to='2030-01-01')}} // '["2024-10-15 07:24:57","2026-10-15 05:30:35"]'
{{$date.betweens(from='2020-01-01',to='2030-01-01',min=2,max=5)}} // '["2020-06-28 22:41:07","2025-10-14 12:09:30"]'
birthdate
Returns a random birthdate. By default, the birthdate is generated for an adult between 18 and 80 years old. But you can customize the 'age'
range or the 'year'
range to generate a more specific birthdate.
Parameters
Name | Type | Default | Description |
---|---|---|---|
refDate | string | number | Date | {{$date.now}} | The date to use as reference point for the newly generated date. |
max | number | The maximum age/year to generate a birthdate for/in. | |
min | number | The minimum age/year to generate a birthdate for/in. | |
mode | 'age' | 'year' | Either 'age' or 'year' to generate a birthdate based on the age or year range. | |
refDate | string | number| Date | {{$date.now}} | The date to use as reference point for the newly generated date. Only used when mode is 'age' . |
Returns: Date
Examples
{{$date.birthdate}} // '1989-09-02 05:29:17'
{{$date.birthdate(min=18,max=65,mode='age')}} // '1992-05-07 07:18:05'
{{$date.birthdate(min=1900,max=2000,mode='year')}} // '1922-04-03 22:46:36'
future
Generates a random date in the future.
Parameters
Name | Type | Default | Description |
---|---|---|---|
refDate | string | number | Date | {{$date.now}} | The date to use as reference point for the newly generated date. |
years | number | 1 | The range of years the date may be in the future. |
Returns: Date
Examples
{{$date.future}} // '2025-02-03 16:47:47'
{{$date.future(years=10)}} // '2029-09-28 19:16:17'
{{$date.future(years=10,refDate='2020-01-01')}}// '2028-04-30 19:23:10'
month
Returns a random name of a month.
Parameters
Name | Type | Default | Description |
---|---|---|---|
abbreviated | boolean | false | Whether to return an abbreviation. |
context | boolean | false | Whether to return the name of a month in the context of a date. In the default en locale this has no effect, however, in other locales like fr or ru , this may affect grammar or capitalization, for example 'январь' with { context: false } and 'января' with { context: true } in ru . |
Returns: Date
Examples
{{$date.month}} // 'April'
{{$date.month(abbreviated=true)}} // 'Jun'
{{$date.month(context=true)}} // 'April'
{{$date.month(abbreviated=true,context=true)}} // 'Sep'
past
Generates a random date in the past.
Parameters
Name | Type | Default | Description |
---|---|---|---|
refDate | string | number | Date | {{$date.now}} | The date to use as reference point for the newly generated date. |
years | number | 1 | The range of years the date may be in the past. |
Returns: Date
Examples
{{$date.past}} // '2024-02-03 10:58:19'
{{$date.past(years=10)}} // '2022-04-18 10:25:51'
{{$date.past(years=10,refDate='2020-01-01')}} // '2010-01-24 17:41:23'
recent
Generates a random date in the recent past.
Parameters
Name | Type | Default | Description |
---|---|---|---|
days | number | 1 | The range of days the date may be in the past. |
refDate | string | number | Date | {{$date.now}} | The date to use as reference point for the newly generated date. |
Returns: Date
Examples
{{$date.recent}} // '2024-08-29 22:07:48'
{{$date.recent(days=10)}} // '2024-08-29 16:06:18'
{{$date.recent(days=10,refDate='2020-01-01')}} // '2019-12-29 07:31:50'
soon
Generates a random date in the near future.
Parameters
Name | Type | Default | Description |
---|---|---|---|
days | number | 1 | The range of days the date may be in the future. |
refDate | string | number | Date | {{$date.now}} | The date to use as reference point for the newly generated date. |
Returns: Date
Examples
{{$date.soon}} // '2024-08-31 06:08:01'
{{$date.soon(days=10)}} // '2024-08-31 07:38:28'
{{$date.soon(days=10,refDate='2020-01-01')}} // '2020-01-04 04:34:40'
timeZone
Returns a random IANA time zone name.
The returned time zone is not tied to the current locale.
Returns: string
Examples
{{$date.timeZone}} // 'Europe/Zagreb'
weekday
Returns a random day of the week.
Parameters
Name | Type | Default | Description |
---|---|---|---|
abbreviated | boolean | false | Whether to return an abbreviation. |
context | boolean | false | Whether to return the day of the week in the context of a date. In the default en locale this has no effect, however, in other locales like fr or ru , this may affect grammar or capitalization, for example 'Lundi' with { context: false } and 'lundi' with { context: true } in fr . |
Returns: string
Examples
{{$date.weekday}} // 'Sunday'
{{$date.weekday(abbreviated=true)}} // 'Sun'
{{$date.weekday(context=true)}} // 'Monday'
{{$date.weekday(abbreviated=true,context=true)}} // 'Mon'