Correct way to create random dates with luxon and fakerjs #1252
-
How to create a random date using import Factory from '@ioc:Adonis/Lucid/Factory'
import { DateTime } from 'luxon'
import Holiday from 'App/Models/Holiday'
export default Factory.define(Holiday, ({ faker }) => {
return {
date: DateTime.fromObject({ ordinal: faker.random.number(364) + 1 }),
}
}) This form does not work for leap years. How to make it work for any date? Note: Adonis v5 |
Beta Was this translation helpful? Give feedback.
Answered by
RomainLanz
Jul 16, 2020
Replies: 2 comments 3 replies
-
Have you tried to directly use ChanceJS chance.date()
chance.date({ string: true })
chance.date({ string: true, american: false })
chance.date({ year: 1983 }) |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
fnoquiq
-
You can achieve using import factory from '@adonisjs/lucid/factories'
import { DateTime } from 'luxon'
import Holiday from '#models/holiday'
export const HolidayFactory = factory.define(Holiday, ({ faker }) => {
return {
date: DateTime.fromJSDate(faker.date.anytime()),
}
}).build() |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Have you tried to directly use ChanceJS
date
function?