-1

I tried to display date in LAO PDR ພາສາລາວ format but not works.

According to text my code is

// 4.6.2024, 23:26:55 this de (German is correct)
console.log(new Date().toLocaleString("de")); 

// expect 4/6/2024, 23:26:55 but it 
// shows 6/4/2024, 11:26:55 PM which is not correct
console.log(new Date().toLocaleString("lo")); 

I also tried full tag "lo-LA" and by LICD "1108" from LAO PDR

What's wrong with my code?

Thanks in advance everyone

1
  • I suspect whatever implementation you're running the code in doesn't support language "lo" so defaults to en-us. Try a different implementation (e.g. run it in Safari, Firefox or Chrome). Commented Jun 8, 2024 at 9:41

5 Answers 5

1

I didn't quite understand what exactly you need but based on the code you provided, and the reference to the respective library, my guess is that you don't need any third-party modules since JavaScript itself has provided us with the "Internationalization API" internally, accessing with Intl Class:

try this code:

console.log(
    Intl.DateTimeFormat('lo-LA', {
        hour: '2-digit',
        minute: 'numeric',
        second: '2-digit',
        year: 'numeric',
        month: 'numeric',
        day: 'numeric',
    }).format(new Date()));

Keep in mind, that you can also use timeZone: 'UTC', option in the options object (second argument) to change the time zone, try for yourself.

The output will be something like 4/6/2024, 20:36:26

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

Comments

0

According to MDN, not all localisations are supported. In your example, this might be a case of Lao not being included in the supported locales therefore, defaulting to the system's locale (which may not be what you want.)

You are able to provide a fallback when the requested locale is not supported.

console.log(new Date().toLocaleString(["lo", "id"]));

In the example above, I provided Indonesian as a fallback as it is similar to Lao's date format. You could also use British (en-GB) fallback.

Note, the time format might not be what you want if you go with the Indonesian fallback.

1 Comment

thanks, I use en-GB locale like your answer which is similar to Laos
0

Technically, instead of using .toLocaleString() you should use .toLocaleDateString() for dates, which yields...

new Date().toLocaleDateString('lo-LA')

And my output now becomes...

2024-6-4

Which is the reverse of what you expected, but then again maybe the fact that my operating system's locale is different to yours may make a difference.

But anyway, I then use options to see if I can change it...

let Options = {
 day: 'numeric',
 month: 'numeric',
 year: 'numeric',
};


alert( new Date().toLocaleDateString('lo-LA',Options) ); //expect 4/6/2024

But it doesn't seem to put them in that order! :(

So I turn to the Wiki article... "List of date formats by country"

https://en.wikipedia.org/wiki/List_of_date_formats_by_country

But there's no info for Laos, so we're stuck! :(

1 Comment

The format from toLocaleString is based on the language parameter (misnamed "locale"), other options only affect the values, not their order. Formats are supposed to be consistent with those at the CLDR, but may vary across implementations. In Safari and Firefox, new Date().toLocaleDateString('lo-LA') returns "8/6/2024".
0

Country Laos supports the default date format as Date Month and Year format. Hence the format is printed is correct. However you can change the format to display in differently with options as below

const options = { year: "numeric", month: "numeric", day: "numeric", }; console.log(new Date().toLocaleString('lo', options));

1 Comment

Options change the values, not the order. The OP already has numeric values for year, month and day.
0

As others noted: Should the localisation for Laos not be supported, you may be able to use a fallback locale similar to Laos's instead.

Alternatively, you can format it yourself, e.g. with the ECMAScript Internationalization API (Intl namespace).

Example:

const date = Date.UTC(
  2024, 6, 4,
  23, 26, 55
);

console.log(formatAsLaos(date));

function formatAsLaos(date) {
  const formatter = Intl.DateTimeFormat("lo-LA", {
      // Use Laos' timezone; also try "UTC"
      timeZone: "Asia/Vientiane",

      // Ensure expected format
      calendar: "gregory",
      numberingSystem: "latn",
      hour12: false,
      dateStyle: "medium",
      timeStyle: "medium"
    });
    
  const formatParts = formatter.formatToParts(date);
  // (Convenience function)
  const getPart = (partType) => formatParts.find(({type}) => type === partType)?.value;

  const dateString = [getPart("day"), getPart("month"), getPart("year")].join(".");
  const timeString = [getPart("hour"), getPart("minute"), getPart("second")].join(":");

  return `${dateString}, ${timeString}`;
}

Comments

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.