-
Notifications
You must be signed in to change notification settings - Fork 119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
getDateObject doesn't recognize configured date format #980
Comments
At the moment the date is parsed like documented here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date
But the "normal user" cannot know how this works internally. They just know their local date formats. var momentDate = moment('04.03.2022', 'DD.MM.YYYY'); The problem is, that the same blockly / javascript would work differently when the system configuration changes. |
"The problem is, that the same blockly / javascript would work differently when the system configuration changes." In fact the same error still exists. I have now also switched to momentjs() in combination with the “moment-parseformat” plugin. The problem that the same Blockly/Javascript would work differently if the system configuration changes does not exist either... It would still be good if the functions could be corrected. Here is a short example of the problem and a possible alternative solution const moment = require('moment');
const parseFormat = require('moment-parseformat');
const targetFormat = 'DD.MM.YY';
moment.locale('de-DE'); //aus den 0_userdata locals oder aus dem System
for (let i=1; i<33; i++) {
//mal alle Tage über den gesamten Monat zusammenbauen
let Tag: string = '';
if (i < 10) {
// 0 davor, sonst gibts ne falsche Prüfung
Tag = '0' + String(i) + '.11.2023 00:00'; // moment-parseformat findet das Format zur Prüfung auch mit Uhrzeit
}
else {
Tag = String(i) + '.11.2023'; // oder ohne Uhrzeit --> oder völlig anders
}
// 31.11.2023 dürfte es nicht geben (also isValid = false) und dann kommt der Käse
if (i==32) {
Tag = 'Käse'; // um zu überprüfen ob 'Käse' auch als Datum validiert wird!!!
}
var pformat = parseFormat(Tag);
console.log(pformat);
// JS-Methode - nur zum Vergleich --> wird nicht sauber arbeiten!!!
console.log('Date.parse --> Ist das Datum ' + Tag + ' valid?: ' + !isNaN(Date.parse(Tag)));
// Erkennt das Datum
console.log('moments.js --> Ist das Datum ' + Tag + ' valid?: ' + moment(Tag, pformat, true).isValid());
if (moment(Tag, pformat, true).isValid()) { //Sting ist Datum in diesem Format
let DatumZeit = moment(Tag, pformat).unix(); // Umwandlung in Unix Time-Stamp
console.log('Umwandlung in Unix-Timestamp: ' + DatumZeit);
console.log('Formatierung über JS in Zielformat: ' + formatDate(DatumZeit, targetFormat));
}
else { // der String wird ausgegeben
console.log(i + ' x ' + Tag);
}
} show logs:
|
Describe the bug
When executing
The result is
03.04.2022
ioBroker.javascript/lib/sandbox.js
Lines 2476 to 2490 in 58daed0
The text was updated successfully, but these errors were encountered: