Exploration of the wildcard character in Moment.js parsing

Is there a way in momentjs to strictly parse input while accepting wildcards?

To strictly parse input, pass true as the third parameter.

I want to support date formats like DD/MM/YYYY, DD-MM-YYYY, DD.MM.YYYY, and DD MM YYYY. Instead of maintaining a list with all possible delimiters, I would like to use wildcards such as * or .. Is it feasible to enforce the date format while disregarding the delimiter?

moment('12/12/2012', ["DD/MM/YYYY", "DD-MM-YYYY", "DD.MM.YYYY", "DD MM YYYY"], true).isValid() === true;

// Ideally ignoring delimiter type, this should evaluate to false
moment('12/12/2012', "DD*MM*YYYY", true).isValid() === false;

Answer №1

Opposing strict parsing is the concept of using wildcards. For instance, it is best to avoid accepting '01/01-2020'.

This is precisely the purpose of the specific variant you are currently utilizing (strict multi-format).

In cases where strictness is not required, the information provided in the documentation is quite explicit:

The parser disregards characters that are non-alphanumeric, therefore both examples below will yield the same result.

moment("12-25-1995", "MM-DD-YYYY");
moment("12/25/1995", "MM-DD-YYYY");

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The placement of the breadcrumb trail is not in the right position

My website includes a menu and breadcrumbs. The menu consists of two submenus: Design Services, Design Portfolio, Design Fees, About Us, Contact Us, Design Resources, Design Vacancies, and Terms. Clicking on Design Services reveals Graphic Design, Logo Des ...

The md-nav-bar is not displaying properly, it is only showing plain text with no

The Angular Material directive "md-nav-bar" is causing trouble in my code. It refuses to render, even after trying various snippets of working code that I found. HTML <body ng-app="app" ng-controller="MainController"> <md-content layout="row"&g ...

Issue with setting GeoJSON in jQuery callback using Mapbox`

Using Jquery, I am trying to retrieve geojson points data (which will set markers on a map) from a database. function remm(id){ $.ajax({ type: "POST", url:"geojson2.php", data:{ u_id:id[0], t_id:id[2] }, success: functi ...

How to programmatically close a Liferay dialog box

I am currently dealing with a Liferay dialog box. My goal is to close this dialog box and then redirect the URL to a specific page. This is how I am attempting to achieve it: <aui:column columnWidth="16" > <%if(UserGroupRoleLocalServiceUtil.has ...

When the button is clicked, request the total count of elements in the array

Is there a way to log the index of an array element when clicked? I have a large array with over 100 elements: var cubesmixed = []; var cubes; for(var i = 0; i < 143; i++) { cubes = paper.rect(Math.floor(Math.random()*2000), Math.floor(Math.random ...

Steps for transforming a numerical value into an array with individual elements, such that the maximum value in the array will be 1

Can someone assist me? I have a value of 4.8 (it represents a rating of 4.8/5). Now I want to convert it into an array with the following structure: [1, 1, 1, 1, 0.8] What I mean is that the value should be split into 5 elements, with each element not ...

Retrieve the screen width using a JavaScript function and apply it as a percentage

I find myself needing to adjust the size of table elements based on the width of the screen. While I am not well-versed in javascript or html, resolving this issue is crucial. Unfortunately, I did not create the original asp page and have limited ability t ...

What is the best way to extract values from this PHP script?

I recently delved into the d3 javascript library and successfully created a scatter plot chart that pulls random values from an array. Below is the code snippet: <!DOCTYPE html> <meta charset="utf-8"> <head> <script type="text/ja ...

What is the method to determine if a date is larger or smaller than another using Javascript?

Using the inputText function retrieves the value entered in the textbox, while the hidden field value returns the current value. This is my current code snippet: if (inputText.value.length != 0) { if (inputText.value < document.getElementById(&apo ...

Unable to retrieve all AJAX responses during page loading in Puppeteer

When loading the URL here, I notice around four AJAX calls in the network tab. However, when using Puppeteer to scrape the data, only one response URL from an AJAX call is being logged: I have attempted the following code: async function main() { try { ...

Is it possible to refresh a div with just one click on an image?

I've tried searching, but I can't seem to find a clear explanation. Here's what I'm trying to achieve: I want to refresh specific divs on my webpage without having to reload the entire page. For example: <div id="topimage"> < ...

The height map method for plane displacement is experiencing issues

The heightmap I have selected: Scene without grass.jpg map : Scene with grass.jpg map: https://i.sstatic.net/q6ScO.png import * as THREE from 'three'; import {OrbitControls} from 'three/examples/jsm/controls/OrbitControls.js'; import ...

I am interested in deleting an element from Firebase using JavaScript

I'm struggling to grasp the concept of deleting an item from my Firebase database. I have successfully created a function (saveEmployee) to add items, but removing them is where I hit a roadblock. HTML <tbody ng-repeat="employee in employees"> ...

Using JavaScript functions in ASP.NET applications

Hey there! I'm having some trouble with my aspx file. I want to call a function called "hello world" from the 'test.js' file, but it's not working. Can anyone suggest a better way to do this? <asp:ScriptManager ID="ScriptManager1" r ...

transfer data from local array to global variable

Need help with returning array values using console.log(array);, currently it's displaying empty value []. Any tips or suggestions would be greatly appreciated. var array = []; var maxLength = 3; var delay = 250; //Shortened the delay var ticker = {}; ...

Strategies for Mitigating Duplicate Requests in AngularJs with just one click

I am facing an issue with my AngularJS application where clicking on a link triggers the API call twice. I'm not sure why this happens and how to fix it. My service: SomethingService function getData() { return apiSettings.getApiInformation().t ...

Angular - Displaying a notification when the timezone does not align with the current moment in time

I am currently working on an angular project where users are only allowed to create tasks for today or future dates using a date picker. I have implemented the <mat-datepicker> along with moment to disable past dates. <mat-form-field formGroupNa ...

Embed a physical entity within another physical entity

On my webpage, I have 2 toggle buttons - "Leaderboard" and "MedalTally". The layout looks like this: https://i.sstatic.net/IohqA.png Here are the codes for the above page: *, *:before, *:after { box-sizing: border-box; } html { overflow-y: scrol ...

Adding points to a bar or pie chart in a Vue environment can be achieved dynamically by utilizing Vue's reactivity system

Looking to bootstrap a Highcharts bar chart and dynamically add points to it within a Vue container, the documentation references addPoint(), setData(), and update(). However, I struggled to make any of these methods work. The provided demo for updating a ...

Adding images to chart labels in vue-chartjs explained

I'm facing a challenge in adding flag icons below the country code labels, and I could really use some guidance. Here is an image of the chart with my current code The flag images I need to use are named BR.svg, FR.svg, and MX.svg, located under @/a ...