1

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?

staticdev
  • 2,950
  • 8
  • 42
  • 66
  • 1
    Vue makes no difference and we didn't need all the extraneous Vue code – Dexygen Feb 25 '23 at 18:48
  • Does this answer your question? [How do you format a Date/Time in TypeScript?](https://stackoverflow.com/questions/40526102/how-do-you-format-a-date-time-in-typescript) – Dexygen Feb 25 '23 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. – staticdev Feb 25 '23 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 ``. – starball Mar 04 '23 at 18:24
  • @user are right.. I made further simplifications. – staticdev Mar 04 '23 at 20:59

1 Answers1

1

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.

starball
  • 20,030
  • 7
  • 43
  • 238
  • 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. – staticdev Mar 04 '23 at 09:24
  • 1
    "_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. – starball Mar 04 '23 at 09:36
  • this is referenced in some places eg. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString or https://en.wikipedia.org/wiki/Date_format_by_country – staticdev Mar 04 '23 at 09:41
  • 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) – starball Mar 04 '23 at 16:38