Date-FNS, format date #197768
-
🏷️ Discussion TypeQuestion BodyI am using Date-FNS and need to format the input field, type="date" defaultValue={formattedDate} to dd-MMM-yyyy. Desired input: i.e. 02-JUL-2026. I am getting: 2026-07-02.
Guidelines
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
This comment was marked as spam.
This comment was marked as spam.
-
|
Use You can keep two formats if you need both: const currentDate = new Date()
const inputDate = format(currentDate, 'yyyy-MM-dd')
const displayDate = format(currentDate, 'dd-MMM-yyyy').toUpperCase()Then use <input type="date" defaultValue={inputDate} />If you want the user to see |
Beta Was this translation helpful? Give feedback.
-
|
Hey — the issue here isn't date-fns, it's working exactly right. The problem is a fundamental constraint of the HTML spec itself.
The browser controls the visual display, not JavaScript. What you actually see in a date input (whether it renders as Your date-fns code is correct — you just need to use the formatted value separately for display, not as the input's value. Here's the pattern: import { format, parseISO } from 'date-fns';
// The raw value the date input works with — must be yyyy-MM-dd
const rawDate = '2026-07-02';
// Formatted for display purposes only
const displayDate = format(parseISO(rawDate), 'dd-MMM-yyyy').toUpperCase();
// → "02-JUL-2026"The practical fix depends on your use case:
<span>{displayDate}</span>
<input type="hidden" name="date" value={rawDate} />
The short version: use |
Beta Was this translation helpful? Give feedback.
Hey — the issue here isn't date-fns, it's working exactly right. The problem is a fundamental constraint of the HTML spec itself.
type="date"inputs always useyyyy-MM-dd— full stop. The HTML specification mandates that the value of a date input must be inyyyy-MM-ddformat (e.g.2026-07-02). This is true regardless of what you pass to it. If the value doesn't match that format exactly, the browser treats the field as empty. So when you setdefaultValue="02-JUL-2026", the browser discards it silently.The browser controls the visual display, not JavaScript. What you actually see in a date input (whether it renders as
07/02/2026,02.07.2026, or something else) is determined entirely by th…