diff --git a/src/Umbraco.Web.UI.Client/src/common/services/util.service.js b/src/Umbraco.Web.UI.Client/src/common/services/util.service.js index 1fb5884d4cb3..e69d49d82e6f 100644 --- a/src/Umbraco.Web.UI.Client/src/common/services/util.service.js +++ b/src/Umbraco.Web.UI.Client/src/common/services/util.service.js @@ -62,23 +62,23 @@ function dateHelper() { return { - convertToServerStringTime: function (momentLocal, serverOffsetMinutes, format) { + convertToServerStringTime: function (momentLocal, serverOffsetMinutes, format = "YYYY-MM-DD HH:mm:ss") { //get the formatted offset time in HH:mm (server time offset is in minutes) - var formattedOffset = (serverOffsetMinutes > 0 ? "+" : "-") + + const formattedOffset = (serverOffsetMinutes > 0 ? "+" : "-") + moment() .startOf('day') .minutes(Math.abs(serverOffsetMinutes)) .format('HH:mm'); - var server = moment.utc(momentLocal).utcOffset(formattedOffset); - return server.format(format ? format : "YYYY-MM-DD HH:mm:ss"); + const server = moment.utc(momentLocal).utcOffset(formattedOffset); + return server.format(format); }, - convertToLocalMomentTime: function (strVal, serverOffsetMinutes) { + convertToLocalMomentTime: function (strVal, serverOffsetMinutes, format = "YYYY-MM-DDTHH:mm:ss") { //get the formatted offset time in HH:mm (server time offset is in minutes) - var formattedOffset = (serverOffsetMinutes > 0 ? "+" : "-") + + const formattedOffset = (serverOffsetMinutes > 0 ? "+" : "-") + moment() .startOf('day') .minutes(Math.abs(serverOffsetMinutes)) @@ -88,12 +88,12 @@ function dateHelper() { //otherwise known as https://en.wikipedia.org/wiki/ISO_8601. This is the default format returned from the server //since that is the default formatter for newtonsoft.json. When it is in this format, we need to tell moment //to load the date as UTC so it's not changed, otherwise load it normally - var isoFormat; + let isoFormat; if (strVal.indexOf("T") > -1 && strVal.endsWith("Z")) { - isoFormat = moment.utc(strVal).format("YYYY-MM-DDTHH:mm:ss") + formattedOffset; + isoFormat = moment.utc(strVal).format(format) + formattedOffset; } else { - isoFormat = moment(strVal).format("YYYY-MM-DDTHH:mm:ss") + formattedOffset; + isoFormat = moment(strVal).format(format) + formattedOffset; } //create a moment with the iso format which will include the offset with the correct time @@ -101,16 +101,16 @@ function dateHelper() { return moment.parseZone(isoFormat).local(); }, - getLocalDate: function (date, culture, format) { + getLocalDate: function (date, culture, format, parsingFormat = "YYYY-MM-DD HH:mm:ss") { if (date) { - var dateVal; - var serverOffset = Umbraco.Sys.ServerVariables.application.serverTimeOffset; - var localOffset = new Date().getTimezoneOffset(); - var serverTimeNeedsOffsetting = -serverOffset !== localOffset; + let dateVal; + const serverOffset = Umbraco.Sys.ServerVariables.application.serverTimeOffset; + const localOffset = new Date().getTimezoneOffset(); + const serverTimeNeedsOffsetting = -serverOffset !== localOffset; if (serverTimeNeedsOffsetting) { - dateVal = this.convertToLocalMomentTime(date, serverOffset); + dateVal = this.convertToLocalMomentTime(date, serverOffset, format); } else { - dateVal = moment(date, 'YYYY-MM-DD HH:mm:ss'); + dateVal = moment(date, parsingFormat); } return dateVal.locale(culture).format(format); }