19

With moment.js, you can format a date this way:

const date = moment("2010-10-22T21:38:00");
const data = date.format("LL - LT") 
console.log(data)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.0/moment.min.js" integrity="sha512-Izh34nqeeR7/nwthfeE0SI3c8uhFSnqxV0sI9TvTcXiFJkMd6fB644O64BRq2P/LA/+7eRvCw4GmLsXksyTHBg==" crossorigin="anonymous"></script>

How to do the same thing with Luxon? I've tried:

const date = luxon.DateTime.fromISO("2010-10-22T21:38:00" );
const data = luxon.DateTime.fromFormat(date, "DATETIME_SHORT")
// output => script error
<script src="https://cdn.jsdelivr.net/npm/luxon@1.25.0/build/global/luxon.min.js"></script>
Matt Ellen
  • 11,268
  • 4
  • 68
  • 90
DoneDeal0
  • 5,273
  • 13
  • 55
  • 114

2 Answers2

36
const date = DateTime.fromISO("2010-10-22T21:38:00")
const humanReadable = date.toLocaleString(DateTime.DATETIME_MED);

console.log(humanReadable); // =>  October 22, 9:38 PM

Source: https://github.com/moment/luxon/blob/master/docs/formatting.md

You can read more about the possible formats here: https://github.com/moment/luxon/blob/master/docs/formatting.md#the-basics

PatricNox
  • 3,306
  • 1
  • 17
  • 25
  • 2
    The links are not working any more. Here is an updated version: https://github.com/moment/luxon/blob/master/docs/formatting.md – TheProgrammer Jul 16 '21 at 13:18
  • Uncaught ReferenceError: DateTime is not defined – o_O Oct 03 '21 at 22:52
  • 1
    @oO. Have you referenced to the luxon instance? like so: `const DateTime = luxon.DateTime;` or `const { DateTime } = require("luxon");` – PatricNox Oct 04 '21 at 07:34
  • https://moment.github.io/luxon/demo/global.html Passionate developers created a lot of examples. :) – shrekuu Aug 25 '22 at 14:42
1
const newDate = DateTime.fromISO(value)
    .setLocale(LuxonSettings.defaultLocale)
    .toLocaleString('datetimeFull');

You can find extra examples for Luxon here: https://moment.github.io/luxon/demo/global.html

And extra date formats here: https://www.jsdocs.io/package/@types/luxon

Bullsized
  • 362
  • 4
  • 7