The issue with the date format in JavaScript appears to be malfunctioning

I modified the script. Here is the updated function:

    function Date(stringDate){
        var date=moment(stringDate).locale('es').format('DD MMM');
        if(date=="Invalid Date"){
            return '-';
        } else {
            return date;
        }
    
    }

Unfortunately, it continues to output the date in English.

Answer №1

Please take note of the warnings in the comments regarding the incorrect version for Spanish translation.

If you are still inclined to proceed, the simplest method would be to utilize a regular expression replacement on the output from the previous step. The provided code snippet offers an alternative approach with the addition of a replace function. It also permits the input of a date, defaulting to today's date if none is specified:

let formatDate = (date = new Date()) => date == "Invalid Date" 
  ? '-' 
  : date.toLocaleDateString('es-ES', {day: 'numeric', month: 'short'})
    .replace(/\b([a-z])/, (s, w) => w.toUpperCase())
    
console.log(formatDate())
console.log(formatDate(new Date(1776, 7, 4)))
console.log(formatDate(new Date(1862, 4, 5)))
console.log(formatDate(new Date('foo-bar-baz')))

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

Entering data into a webpage and retrieving the result from a designated cell in a Google Sheets document

Currently, I am in the process of developing a school platform and I am looking to incorporate a functionality that allows students to easily check the number of "passport" points they have earned. I envision having an input field along with a button that ...

javascriptJQuery validation function triggers JSF ajax request

I am currently experiencing a challenge with JSF ajax requests and jQuery events. I am using a jQuery event to validate a section of a form like this: jQuery(".btnNextStep").live("click", function(e) { var error = !validate(id); ... ...

Console not displaying any logs following the occurrence of an onClick event

One interesting feature I have on my website is an HTML div that functions as a star rating system. Currently, I am experimenting with some JavaScript code to test its functionality. My goal is for the console to log 'hello' whenever I click on ...

establish a compacted directory shortcut within alfresco

Although I'm not highly skilled in Javascript, I am in need of creating a webscript for Alfresco that can generate an alias [A] for a folder (space) [B]. If the structure of [B] appears as companyhome/site/special/my/fo/ld/de/r, [A] should be struct ...

Nested function and the process of breaking out of the parent function

Is it possible to exit out of the main/parent function when I am in a function that was called from inside another function? For example: function(firstFunction(){ //stuff secondFunction() // code continuation if second function doesn't ...

Showing the user's current location on a MapView in React Native

I'm having trouble setting the initial location of a MapView in my React Native app to the device's geolocation. Currently, I'm using a ref to update the coordinates with the help of the geolocation package. However, I'm facing an issu ...

Can AngularJs function effectively without the use of bootstrap?

As I begin my journey into Angular, I can't help but notice that most tutorials start with bootstrap for bootstrapping. While I understand the appeal of grid systems and CSS frameworks, I personally prefer a more minimalist approach. So my question is ...

webpack is failing to load SVG files

My application structure includes: /webapp /lib /assets ic_add_black_24px.svg ic_clear_black_24px.svg .. .. The configuration in my webpack.config.js looks like this: var path = require('path'), webpack = requ ...

Iterate over each item in the select and include a blank option if either of the elements is missing

Having trouble with 2 drop down menus, select1 and select2. I select item1 from select1 and then click the OK button. But when the selected index is 0 (first option), I want to loop through the items of both drop downs. If the elements at the same index ( ...

Automating web interactions with Python and Selenium to simulate clicking a button on a webpage

click here to see the image On a quest to connect to and hit the "good" button located 3/4 of the way down. I attempted to use Selenium's get_element(By.xpath) method, but upon inspecting the elements, I noticed that all buttons appear identical in ...

Extract the month, day, and hour information from a given string

I am working with a Data Frame that contains dates in the format 2014-06-10 06:12:35 BRT. I need to compare these dates to determine if they fall within the same social day, which is defined as between 3:00 am one day and 3:00 am the following day. However ...

What is the best way to include a php file within a div element?

I'm attempting to reload a specific div element only. $("#captcha").html('<?php echo 'abc';?>'); // This is just a test and works fine Since the content of the div is quite large, I attempted: $("#captcha").html('< ...

The content of XMLHttpRequest is accessible via the property response

Typically, when we want to retrieve data using AJAX, we would use code like this: var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function(){ if(xhr.readyState == 4 && xhr.status == 200){ elem.innerHTML = xhr.responseText; ...

What is the best way to delete a subdocument from a main document when either condition x is true or condition y is true?

A subdocument referred to as Bets is embedded into an array of a property on another model called Users. When a user removes a friend, all associated bets with that specific user should also be deleted from the User document (the user who was friends with ...

Enhancing Javascript performance with the power of the V8 engine and Typescript

I recently came across an informative article discussing V8 engine and javascript optimization. How JavaScript works: inside the V8 engine + 5 tips on how to write optimized code The article highlights some key recommendations: a. V8 engine utilizes hid ...

Automatically close an element when you click on a different element

Hello everyone! I need some help again. I'm working on a script that displays an overlay when a menu item is clicked. Within this menu, there is a modal contact form with a close icon or anchor that should appear when clicked. My goal is to have the o ...

Tips on effectively validating a form using JavaScript

I have been attempting to implement form validation using javascript. Below is the code I am using: <script type="text/javascript"> function validateForm(){ var form = document.forms[0]; var password1 = form.elements["passwd" ...

Version 1.0 and higher of Ionic do not display the side menu when using tabs

I have developed an app using Ionic that includes a side menu with tabs feature. The side menu displays correctly when using Ionic version 0.9.27, but it does not show up when the version is 1.0 or higher. What could be causing this issue? HTML Layout & ...

Is it possible for me to modify the text in the address bar without leaving the current page?

My client asked me to create a search page, but the challenge is that the actual search functionality is within an iframe, and the user sees a decorative shell around it. The shell passes URL parameters to the iframe, which processes them and carries out t ...

I am looking for a way to prevent the dot (.) from being registered when a user presses a

I've created a jQuery function that allows for only dots (.) and rejects all other symbols and alphabets. However, I'm not sure why it's not functioning as expected. Can someone please assist me with this issue? Thank you. $('.Number& ...