DateFunctionsSrv.js
| angular.module('myApp', ['myApp.service']).provider('DateFunctionsSrv', function (DateSrv) { | |
| this.regex = {}; | |
| this.setRegexFormats = function (regexFormats) { | |
| this.regex.DateTimeFullFormatRegex = regexFormats.DateTimeFullFormatRegex; | |
| this.regex.DateTimeRegex = regexFormats.DateTimeRegex; | |
| this.regex.DateRegex = regexFormats.DateRegex; | |
| }; | |
| this.ConvertDateStringsToDates = function (input) { | |
| // Ignore things that aren't objects. | |
| if (typeof input !== "object") return input; | |
| for (var key in input) { | |
| if (!input.hasOwnProperty(key)) continue; | |
| var value = input[key]; | |
| var matchDateTime; | |
| var matchDate; | |
| // Check for string properties which look like dates. | |
| if (typeof value === "string") { | |
| var matchedDateTime = value.match(this.regex.DateTimeFullFormatRegex); | |
| if (!matchedDateTime) | |
| matchedDateTime = value.match(this.regex.DateTimeRegex); | |
| if (value.length == 34 && (matchDateTime = matchedDateTime)) { | |
| if (matchDate = value.match(this.regex.DateRegex)) { | |
| //If has only date component no timezone conversion is applied | |
| var date = new Date(new Date(value.slice(0, -1))); | |
| } | |
| else | |
| var date = new Date(matchDateTime[0]); | |
| if (!isNaN(date)) { | |
| input[key] = date; | |
| } | |
| } | |
| } else if (typeof value === "object") { | |
| // Recurse into object | |
| this.ConvertDateStringsToDates(value); | |
| } | |
| } | |
| }; | |
| this.ConvertDatesToDateStrings = function (input) { | |
| // Ignore things that aren't objects. | |
| if (typeof input !== "object") return input; | |
| for (var key in input) { | |
| if (!input.hasOwnProperty(key)) continue; | |
| var value = input[key]; | |
| // Check for string properties which look like dates. | |
| if (value instanceof Date) { | |
| var momentDate = new moment(value); | |
| if (momentDate.clone().startOf('day').isSame(momentDate)) | |
| input[key] = momentDate.format(DateSrv.getDateFormatFromSettings()); | |
| } else if (typeof value === "object") { | |
| // Recurse into object | |
| this.ConvertDatesToDateStrings(value); | |
| } | |
| } | |
| }; | |
| this.$get = function () { | |
| return this; | |
| }; | |
| }); |
| (function () { | |
| function DateSrv($localStorage, DateTimeConstants) { | |
| var DateSrvObj = {}; | |
| DateSrvObj.addAppCultures = function (appCultures) { | |
| $localStorage.appCultures = []; | |
| $localStorage.appCultures = Enumerable.From(appCultures).Select(function (culture) { culture.ShortDatePattern = culture.ShortDatePattern.replaceAll('d', "D").replaceAll('y', "Y"); return culture; }).ToArray(); | |
| }; | |
| DateSrvObj.getDateFormatFromSettings = function () { | |
| var culture = Enumerable.From($localStorage.appCultures).FirstOrDefault(null, function (culture) { | |
| return culture.Name == $localStorage.warehouseSettings.CultureName; | |
| }); | |
| if (culture != null) | |
| return culture.ShortDatePattern; | |
| return culture; | |
| }; | |
| return DateSrvObj; | |
| }; | |
| angular | |
| .module('myApp.service') | |
| .factory('DateSrv', DateSrv); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment