I have this type of date:
"Date": "2024-09-06T06:58:19.326Z",
How should I validate it in my FormRequest in Laravel?
I’ve tried this:
'required|date_format:Y-m-dTH:i:s.uZ'
and this:
date_format:Y-m-d
but I get 422 every time.
0
u
from the date format represents microseconds (6 digits). To match your datetime format you need to use v
which represents only milliseconds (3 digits).
Therfor the solution would be:
'required|date_format:Y-m-dTH:i:s.vZ'
See: https://www.php.net/manual/de/datetime.format.php
Simply adding this will resolve your issue,
Solution 1:
'required|date',
But if you need some exact solution you can do something like this,
Solution 2:
'required|date_format:Y-m-dTH:i:s.vZ',
But the recommended solution is the first one, since it handles most of the date formats. Hope this resolves your issue.
'date' => [
'required',
'regex:/^d{4}-d{2}-d{2}Td{2}:d{2}:d{2}.d{3}Z$/'
]
1
Try This to valiadte your this date “2024-09-06T06:58:19.326Z” format
'date_field' => [
'required',
'date_format:Y-m-dTH:i:s.uZ' // Custom format for ISO 8601 with milliseconds and timezone designator
],
2