@genexus/web-standard-functions
Version:
GeneXus JavaScript standard functions library for web generators
67 lines • 2.08 kB
JavaScript
import { padLeft } from "../text/padLeft";
import { toFormattedString as dateToFormattedString } from "../date/toFormattedString";
export const toFormattedString = (dateFrom, dateFormat, timeFormat, picture) => {
if (!dateFormat) {
dateFormat = "MDY";
}
if (!timeFormat) {
timeFormat = 12;
}
if (!picture) {
picture = "99/99/99 99:99";
}
let datePart = "";
let datePartPicture = picture.split(" ")[0];
datePart = dateToFormattedString(dateFrom, dateFormat, datePartPicture);
let timePartPicture = picture.split(" ")[1];
let timePart = "";
let type = "";
let hours = padLeft(String(dateFrom.getHours()), 2, "0");
let minutes = padLeft(String(dateFrom.getMinutes()), 2, "0");
let seconds = padLeft(String(dateFrom.getSeconds()), 2, "0");
let milliseconds = padLeft(String(dateFrom.getMilliseconds()), 3, "0");
// timeFormat: 12 timeFormat: 24
if (timeFormat === 12) {
if (Number(hours) < 12) {
type = "AM";
}
else {
type = "PM";
}
hours = String(Number(hours) % 12 || 12);
if (hours.toString().length === 1) {
hours = "0" + hours;
}
}
if (!timePartPicture) {
timePart = "";
}
else {
switch (timePartPicture.split(":").length) {
case 1:
timePart = hours;
break;
case 2:
timePart = hours + ":" + minutes;
break;
case 3:
timePart = hours + ":" + minutes + ":" + seconds;
if (timePartPicture.split(".").length === 2) {
timePart = timePart + "." + milliseconds;
}
break;
}
}
if (timePart === "") {
return datePart;
}
else {
if (type !== "") {
return datePart + " " + timePart + " " + type;
}
else {
return datePart + " " + timePart;
}
}
};
//# sourceMappingURL=toFormattedString.js.map