MomentJS only Spanish will translate 'Invalid date' to local - momentjs

Below is simple code of moment.js
const moment = require('moment')
moment.locale('es') // 'it', 'fr', 'zh', 'ja'..
const myDate = moment.utc('').format('MM/DD/YYYY')
console.log(myDate)
Only moment.locale set 'es' it will return 'Fecha invalid' apparently it's Spanish.
But no matter what language I set, like 'it' 'fr' 'zh', it will return 'Invalid date' which is English.
So my question is: why moments handle Spanish differently?

Invalid date originally was a way to handle errors, not locale related, but it changed here
I would guess that's why is not updated with all the locales, so the only option before having updated locales is doing it manually:
moment.locale('it'); // 'it', 'fr', 'zh', 'ja'..
moment.updateLocale("it", {
invalidDate: "data non valida"
});
const myDate = moment.utc('').format('MM/DD/YYYY');
$('#it').text(myDate)
Italian Translation: <span id="it"></span>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment-with-locales.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

You also need to format the locale - eg. see here: moment().format('L'); // 07/28/2021 the locale formatting is separate to the normal date formatting as per https://momentjs.com

Related

How to get the actual meridiems in luxonjs?

So i tried to get the meridiems on luxon.js because i'm going to move my discord.js bot that is using momentjs to luxonjs because i like it more. But i got the problem that i just can't figure out how to get the meridiems of the timezone that i especify, could you help me out?
I've tried
Info.meridiems()
but i don't know how to use or what do i do with the Info part,
And then i don't understand the parameters that the give in their documentation as an example
Info.meridiems({ locale: 'my' })
it was kinda easy but i will not delete this post if someone needs it.
Basically what i did is to get the date in the format that i wanted
const time = DateTime.local().setZone('tz').toFormat("HHmmss");
in .setZone('tz') you should just put the time zone you want according to the luxon docs.
Then i would just use an if for it
const smartMeridiems = (am, pm) =>{
if(time > 120000){
pm = 'PM'
return pm;
} else{
am = 'AM'
return am;
}
}
And that is basically it
The meridiem for a DateTime can be accessed by the "a" format token:
DateTime.local().setZone(z).toFormat("a") //=> "PM"
The Luxon Info methods are for finding out what the meridiems are called in different human languages.

datesDisabled Bootstrap-DatePicker

There are plenty of questions on this but none of them seem to have worked for me.
I will eventually have an array of dates being passed in for multiple days over different months.
I'm using this version of the bootstrap datepicker
https://bootstrap-datepicker.readthedocs.io/en/latest/
looking over the docs, a simple array of dates for'disabledDates' should be all it needs to get these working, but I'll be damned if it accepts it.
Any ideas as to why it doesn't work?
var disabledDates = ['28/3/2018','22/3/2018'];
$('.datepicker').datepicker({
format: 'DD/MM/YYYY',
maxViewMode: 1,
todayBtn: "linked",
clearBtn: true,
multidate: true,
daysOfWeekDisabled: "0,6",
datesDisabled: disabledDates
})
In disabledDates variable you used dates like '28/3/2018' and '22/3/2018'. According to the doc these dates are in this format d/m/yyyy or dd/m/yyyy.
But, inside datepicker() function you have declared the date format as DD/MM/YYYY.
Try to change the date format format: 'DD/MM/YYYY', to format: 'd/m/yyyy', or format: 'dd/m/yyyy',

Momentjs is saying a non valid date is valid

We have an address, that according to momentjs is reporting as valid.. any thoughts?
You can see the behavior in this fiddle
var wrong_date = "7840 W HICKS STREET";
document.write(moment(wrong_date, "YYYY-MM-DD HH:mm:ss").isValid()); //returns true
https://jsfiddle.net/jeffbeagley/2c9urj5v/1/
Use strick format parsing:
var wrong_date = "7840 W HICKS STREET";
document.write(moment(wrong_date, "YYYY-MM-DD HH:mm:ss", true).isValid());
Notice the third boolean parameter set to true.
This is a strick match on the format.
Moment's parser is very forgiving, and this can lead to undesired behavior. As of version 2.3.0, you may specify a boolean for the last argument to make Moment use strict parsing. Strict parsing requires that the format and input match exactly, including delimeters.
document.write(moment(wrong_date, "YYYY-MM-DD HH:mm:ss").isValid()); //returns true
document.write(moment(wrong_date, "YYYY-MM-DD HH:mm:ss", true).isValid()); //returns false
You can use both locale and strictness.
moment('2012-10-14', 'YYYY-MM-DD', 'fr', true);

Symfony, how to get localized date

most likely a silly question, but I do not see how to do.
Within a controller, I need to use $myObj->getData()->format('d-M-Y'), and I wish to get a localized string too (in italian instead than in english).
Within a twig template, I get it by {{ myobj.data|localizeddate('long', 'none', app.request.locale ) }}, but I do not know a similar trick for the former case.
Yes, I found how to do (thanks to ccKep):
$cal = IntlCalendar::fromDateTime($oldObj->getData()->format('d-M-Y')." Europe/Rome");
$newObj->setField("my date is ". IntlDateFormatter::formatObject($cal, "d MMMM YYYY", 'it_IT')."");
In controller you may use Intl. For example:
$intl = new \IntlDateFormatter($request->getLocale(), \IntlDateFormatter::LONG, \IntlDateFormatter::NONE, null, null, 'd-LLL-y');
$date = $intl->format(new \DateTime('now'));

Why moment isValid() API does not support format 'L' for non 'en' local

Moment API isValid seems only support format 'L' for local is 'en':
locale set to 'en':
moment(new Date()).locale('en').format('L') //"04/27/2016"
moment('04/27/2016', 'L', true).isValid() //true
local set to 'zh-cn':
moment(new Date()).locale('zh-cn').format('L') //"2016-04-27"
moment('2016-04-27', 'L', true).isValid() //false, why here is false?
As i don't want to hard code 'MM-DD-YYYY' for 'zh-cn' here, is there any other good way?
Setting the locale like that only affects the current instance of moment, not any future instances. Future instances use the default locale again, which in your case means 'L' is not the YYYY-MM-DD format.
You can either set the locale globally:
moment.locale('zh-cn');
moment(new Date()).format('L') // 2016-04-27
moment('2016-04-27', 'L', true).isValid() // true
Or specify the locale for each instance, including the one you use for parsing:
moment(new Date()).locale('zh-cn').format('L') // 2016-04-27
moment('2016-04-27', 'L', 'zh-cn', true).isValid() // true

Resources