mgm
Version:
My generic modules
35 lines (33 loc) • 1.13 kB
JavaScript
const BaseConverter = require('./BaseConverter');
const Tools = require('../tool/Tools')
module.exports = class DateConverter extends BaseConverter {
constructor() {
super();
}
/**
* Converte ...
* @param {*} stringValue
*/
convertToObject(stringValue) {
let converted;
if (/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d{3}/.test(stringValue)) {
converted = Tools.formatDate(stringValue);
} else if (/\d{4}-\d{2}-\d{2}/.test(stringValue)) {
converted = Tools.formatDate(stringValue, 'YYYY-MM-DD');
} else {
converted = stringValue;
}
return converted;
}
convertToString(objectValue) {
let converted;
if (/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d{3}/.test(objectValue)) {
converted = Tools.formatDate(objectValue);
} else if (/\d{4}-\d{2}-\d{2}/.test(objectValue)) {
converted = Tools.formatDate(objectValue, 'DD/MM/YYYY');
} else {
converted = objectValue + '';
}
return converted;
}
};