javascript - Formatting Strings to Datetime using specific formats in Luxon - Stack Overflow

By now I used moment-js for time handling, but I want to switch to Luxon. But I have problems with the

By now I used moment-js for time handling, but I want to switch to Luxon. But I have problems with the following implementation.

There is a textfield where you can enter a datetime in your locale time format, for instance HH:mm (24h format) or hh:mm (12h format). The used time format is stored in the variable timeFormat.

My solution with moment-js:

let timeFormat = 'HH:mm' // 'hh:mm a'
let textfield = document.querySelector('#input-time');
let timeString = textfield.value;
let dateTime = moment(timeString, timeFormat, true);

// Check if time is valid
if(dateTime.isValid() === false){
    return;
}

Using 12h-format:

  • 11:00 am, 09:43 pm are valid.
  • 11:00, 21:43 are invalid.

Using 24h-format:

  • 11:00 am, 09:43 pm are invalid.
  • 11:00, 21:43 are valid.

How can I aprove obtain a similar solution with Luxon? My biggest issue is to get a similar function to moment(timeString, timeFormat, true), so formatting a String into a Datetime using a specific format, for instance 12h/24-format.

By now I used moment-js for time handling, but I want to switch to Luxon. But I have problems with the following implementation.

There is a textfield where you can enter a datetime in your locale time format, for instance HH:mm (24h format) or hh:mm (12h format). The used time format is stored in the variable timeFormat.

My solution with moment-js:

let timeFormat = 'HH:mm' // 'hh:mm a'
let textfield = document.querySelector('#input-time');
let timeString = textfield.value;
let dateTime = moment(timeString, timeFormat, true);

// Check if time is valid
if(dateTime.isValid() === false){
    return;
}

Using 12h-format:

  • 11:00 am, 09:43 pm are valid.
  • 11:00, 21:43 are invalid.

Using 24h-format:

  • 11:00 am, 09:43 pm are invalid.
  • 11:00, 21:43 are valid.

How can I aprove obtain a similar solution with Luxon? My biggest issue is to get a similar function to moment(timeString, timeFormat, true), so formatting a String into a Datetime using a specific format, for instance 12h/24-format.

Share Improve this question asked Nov 2, 2020 at 9:47 michaelTmichaelT 1,7112 gold badges20 silver badges44 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

You can use fromFormat method

Create a DateTime from an input string and format string. Defaults to en-US if no locale has been specified, regardless of the system's locale.

Example:

const DateTime = luxon.DateTime;
const inputs = ['11:00 am', '09:43 pm', '11:00', '21:43'];
const formats = ['HH:mm', 'hh:mm a'];

const checkTime = (timeString, fmt) => 
  DateTime.fromFormat(timeString, fmt).isValid


for (i=0; i<inputs.length; i++) {
  for (j=0; j<formats.length; j++) {
    console.log(`${inputs[i]} with format '${formats[j]}' is valid? ` + checkTime(inputs[i], formats[j]) );
  }
}
<script src="https://cdn.jsdelivr/npm/[email protected]/build/global/luxon.js"></script>

Have a look at For Moment users section of the manual for good reference to migrate from momentjs to Luxon.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742340214a4425474.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信