@Christian,
Oui, je sais tout ça... Et ça ne répond pas à mon problème... que j'ai finalement résolu par un petit bidouillage.
- Dans l'interface d'édition du contenu H5P (fichier semantics.json) au champ (texte) de saisie de la date j'ai rajouté cette expression régulière qui permet de cadrer la saisie :
- "regexp": {"pattern": "^(-\\d{4,}|\\d{1,}(-(1[012]|[0]?[1-9])(-([12][0-9]|3[01]|[0]?[1-9]))?)?)$"}
- avec cette description: "description": "Pattern: YYYY-MM-DD Only year is required. Years can be negative but year must consist of a minimum of 4 figures (and months and days are not accepted). e.g. -40000, -0045, etc."
- Dans le script qui récupère les données saisies en mode édition, je détecte la présence d'un signe négatif au début de la date; je rajoute un mois et un jour fictifs (1 1, soit le 1er janvier, juste pour que le Date.parse soit content, etc. ce qui donne ce code:
var dateMillis = 0;
if (isNegativeYear) {
var negDateString = '1 1 ' + dateString;
dateMillis = Date.parse(negDateString);
} else {
dateMillis = Date.parse(dateString);
}
Yes, this script should work as intended.
The script first checks if the dateString
starts with a -
character to determine if it represents a negative year. If it does, it creates a new string by adding '1 1 ' to the beginning of the dateString
, which will create a valid date string that can be parsed by the Date.parse()
method.
If dateString
does not start with a -
character, the script simply passes it to the Date.parse()
method.
The Date.parse()
method returns the number of milliseconds since January 1, 1970, 00:00:00 UTC for the given date string. The script stores this value in the dateMillis
variable.
One thing to note is that the var
keyword is used to declare the negDateString
, dateMillis
, and isNegativeYear
variables. In modern JavaScript, it's recommended to use let
or const
instead of var
to declare variables. Additionally, it's worth noting that the Date.parse()
method may not be the best choice for parsing date strings in all cases, as it has some quirks and limitations.