2

I'm trying to format a date in dd/mm/yyyy format in Vue 3 with TypeScript, but the formatting isn't being applied. I saw many answers recommending using moment.js but the documentation of this library said it is outdated and it can be achieved with native toLocaleDateString("en-GB").

Here's my code:

<template>
  <div>
    <label for="date">Date:</label>
    <input type="date" id="date" v-model="selectedDate" />
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from "vue";

const selectedDate = ref("");
</script>

I tried using:

import { ref, computed, watch } from "vue";
// ...
watch(selectedDate, (newValue, oldValue) => {
  const newDate = new Date(newValue);
  const formattedDate = newDate.toLocaleDateString("en-GB");
  selectedDate.value = formattedDate;
});

Also tried adding:

const format = (value: string) => {
  const formatter = new Intl.DateTimeFormat("en-GB", {
    year: "numeric",
    month: "2-digit",
    day: "2-digit"
  });
  return formatter.format(new Date(value));
};
// ...
    <input type="date" id="date" :formatter="format" v-model="selectedDate" />

In both cases, when I enter the page, the date is still rendered as the default format (mm/dd/yyyy).

rendered input

I also tried using the solution from this other question but <input type="date"> does not work properly with string value. I would really like to have the widget for selecting the date.

How can I properly format the date in dd/mm/yyyy format and handle these widget problems, if possible without installing other libraries?

5
  • 1
    Vue makes no difference and we didn't need all the extraneous Vue code Commented Feb 25, 2023 at 18:48
  • Does this answer your question? How do you format a Date/Time in TypeScript? Commented Feb 25, 2023 at 18:48
  • @Dexygen in fact it does not answer. As my question shows, I basically tried what is in the mentioned question but when browser opens the input box is not formatted. Also I am asking specifically with Vue code since it has its on syntactic sugar for input types such as v-model. Also the code is needed so you can reproduce the issue I have. Commented Feb 25, 2023 at 19:07
  • As already stated in one form by dexygen, you can drastically reduce the amount of code needed to understand this question. Technically all the code you really need to show is the fact that you're using <input type="date">. Commented Mar 4, 2023 at 18:24
  • @user are right.. I made further simplifications. Commented Mar 4, 2023 at 20:59

1 Answer 1

3
+50

I'm pretty sure there's no standard way to change the way that date-type HTML <input>s render the order of parts of dates.

Quoting from MDN's page on date inputs:

Note: The displayed date format will differ from the actual value — the displayed date is formatted based on the locale of the user's browser, but the parsed value is always formatted yyyy-mm-dd.

And from How to make <input type="date"> display the date in localized format in Chrome?, the top answer says:

Chrome renders the date on the format for the language configured in Settings > Language:

The fact that moment.js is deprecated is irrelevant- you wouldn't have been able to get what you want using it either.

If you want to control the date formatting of a date input, you either have to write your own date input instead of using <input type="date">, or you could fork Chromium or whatever browser you use and make a modified version of them where that's configurable and ship that to your users. (not practical (I know))

The asker indicated in the comments that they found vuejs3-datepicker useful in solving their problem / achieving their goal.

Sign up to request clarification or add additional context in comments.

4 Comments

I have tried in both Firefox and Brave/Chrome with set language to EN-GB that should show dd-mm-yyyy, but both are rendering the input as mm/dd/yyyy.
"with set language to EN-GB that should show dd-mm-yyyy" says who? As far as my understanding goes, the exact details of how a date is rendered for a specific locale is implementation defined (up to the browser). I may be wrong, but you haven't proved that with facts and references.
From the first link you sent: "Note: Most of the time, the formatting returned by toLocaleString() is consistent. However, the output may vary with time, language, and implementation — output variations are by design and allowed by the specification. You should not compare the results of toLocaleString() to static values." (emphasis added)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.