How to change the format of test dates generated by Bogus in C#

743 Views Asked by At

I'd like to format the test dates generated by Bogus to dd.MM.yyyy (eg. 29.09.2022).

I've been searching for a solution but can't find anything.

I tried the following but it doesn't work:

var personData = new Faker<Person>()
    .RuleFor(
        p => p.DateOfBirth,
        f => f.Date.Past(30, DateTime.Now.AddYears(-20))
        .ToString("dd.MM.yyyy"));

Thanks in advance for any help!

1

There are 1 best solutions below

1
Marcelo GA On

try that:

.RuleFor(c => c.Birthday, f =>
                         {
                             var pastDateTime = f.Date.PastOffset(40, DateTime.Now.AddYears(-18));
                             var result = DateTime.Parse(pastDateTime.ToString("dd.MM.yyyy"));
                             return result;
                         })

enjoy it