Skip to content
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

Allow to specify format and parsing format in dateHelper functions #13500

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions src/Umbraco.Web.UI.Client/src/common/services/util.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -88,29 +88,29 @@ 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
// then convert it to local time
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);
}
Expand Down
Loading