3

I changed the code. I have this function:

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

It still returns the date in english.

jdeveloper
  • 51
  • 3
  • 3
    You won't get that out of that function, because **month names are not capitalized in spanish**. The formatting is doing its job. You _could_ use `string.replace()` to capitalize it yourself. – salezica Nov 06 '18 at 15:57
  • 5
    Unrelated to your locale question, why are you comparing new Date() with the string "Invalid Date"? – jarmod Nov 06 '18 at 15:58
  • @jarmod: See my answer for a possible extension to this function with `new Date()` a default parameter, but allowing for you to supply the date. In this case, `== 'Invalid Date'` actually does the job, even if it is... odd. – Scott Sauyet Nov 06 '18 at 16:19
  • @ScottSauyet OK, seems like an alternative to isNaN(date). I made my original comment, of course, because the given code could not yield an invalid date and it wasn't clear that the OP knew that and had not simply block-copied from somewhere on the internet. – jarmod Nov 06 '18 at 16:23
  • @jarmod—I agree that `isNaN(date)` is a much better idea. – RobG Nov 06 '18 at 23:11

1 Answers1

0

Note the caveats in the comments about this being the incorrect version for Spanish.

But if you still want to do this, using a regular expression replacement on the result of the above is probably the easiest. This is a slightly different formulation of your version, with that replace call added. It also allows you to supply the date, defaulting to today if one isn't supplied:

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')))
Scott Sauyet
  • 49,207
  • 4
  • 49
  • 103
  • If they are using an i18n tool to localize text, I highly recommend parsing the text and modifying it. Will be very sensitive to any changes in code (brittle) – Ruan Mendes Nov 06 '18 at 16:31
  • Absolutely. And I think the code would likely be better for that. This is a very specific solution to a problem that likely should not be fixed. If the OP does want to "fix" the lower-case names issue for that particular Spanish format, this should work. I'd suggest that the OP look to something different, though. – Scott Sauyet Nov 06 '18 at 18:10
  • I really don't understand the attraction of this kind of function expression. A function declaration with clear statements and *if* block is more readable and maintainable, and probably is more efficient. – RobG Nov 06 '18 at 23:15
  • @RobG: I wrote that version to include as a comment and then realized what I wanted to say was too long for that. I don't know if I would choose to change it back, though. I'm very much in favor of removing as much control flow as I can from my code. I've built an [entire library](https://ramdajs.com) on such principles. However, I'm curious as to whether you know of references to the relative efficiencies. – Scott Sauyet Nov 07 '18 at 02:46