ui.date_utils.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /**
  2. * DevExtreme (ui/date_box/ui.date_utils.js)
  3. * Version: 19.1.16
  4. * Build date: Tue Oct 18 2022
  5. *
  6. * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
  7. * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
  8. */
  9. "use strict";
  10. function _typeof(obj) {
  11. "@babel/helpers - typeof";
  12. return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(obj) {
  13. return typeof obj
  14. } : function(obj) {
  15. return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj
  16. }, _typeof(obj)
  17. }
  18. var $ = require("../../core/renderer");
  19. var dateSerialization = require("../../core/utils/date_serialization");
  20. var isDate = require("../../core/utils/type").isDate;
  21. var each = require("../../core/utils/iterator").each;
  22. var dateLocalization = require("../../localization/date");
  23. var dateComponents = function() {
  24. return ["year", "day", "month", "day"]
  25. };
  26. var ONE_MINUTE = 6e4;
  27. var ONE_DAY = 60 * ONE_MINUTE * 24;
  28. var ONE_YEAR = 365 * ONE_DAY;
  29. var getStringFormat = function(format) {
  30. var formatType = _typeof(format);
  31. if ("string" === formatType) {
  32. return "format"
  33. }
  34. if ("object" === formatType && void 0 !== format.type) {
  35. return format.type
  36. }
  37. return null
  38. };
  39. var dateUtils = {
  40. SUPPORTED_FORMATS: ["date", "time", "datetime"],
  41. DATE_COMPONENT_TEXT_FORMATTER: function(value, name) {
  42. var $container = $("<div>").addClass("dx-dateview-formatter-container");
  43. $("<span>").text(value).addClass("dx-dateview-value-formatter").appendTo($container);
  44. $("<span>").text(name).addClass("dx-dateview-name-formatter").appendTo($container);
  45. return $container
  46. },
  47. ONE_MINUTE: ONE_MINUTE,
  48. ONE_DAY: ONE_DAY,
  49. ONE_YEAR: ONE_YEAR,
  50. MIN_DATEVIEW_DEFAULT_DATE: new Date(1900, 0, 1),
  51. MAX_DATEVIEW_DEFAULT_DATE: function() {
  52. var newDate = new Date;
  53. return new Date(newDate.getFullYear() + 50, newDate.getMonth(), newDate.getDate(), 23, 59, 59)
  54. }(),
  55. FORMATS_INFO: {
  56. date: {
  57. getStandardPattern: function() {
  58. return "yyyy-MM-dd"
  59. },
  60. components: dateComponents()
  61. },
  62. time: {
  63. getStandardPattern: function() {
  64. return "HH:mm"
  65. },
  66. components: ["hours", "minutes", "seconds", "milliseconds"]
  67. },
  68. datetime: {
  69. getStandardPattern: function() {
  70. var standardPattern;
  71. ! function() {
  72. var androidFormatPattern = "yyyy-MM-ddTHH:mmZ";
  73. var testDateString = "2000-01-01T01:01Z";
  74. var $input = $("<input>").attr("type", "datetime");
  75. $input.val(testDateString);
  76. if ($input.val()) {
  77. standardPattern = androidFormatPattern
  78. }
  79. }();
  80. if (!standardPattern) {
  81. standardPattern = "yyyy-MM-ddTHH:mm:ssZ"
  82. }
  83. dateUtils.FORMATS_INFO.datetime.getStandardPattern = function() {
  84. return standardPattern
  85. };
  86. return standardPattern
  87. },
  88. components: dateComponents().concat(["hours", "minutes", "seconds", "milliseconds"])
  89. },
  90. "datetime-local": {
  91. getStandardPattern: function() {
  92. return "yyyy-MM-ddTHH:mm:ss"
  93. },
  94. components: dateComponents().concat(["hours", "minutes", "seconds"])
  95. }
  96. },
  97. FORMATS_MAP: {
  98. date: "shortdate",
  99. time: "shorttime",
  100. datetime: "shortdateshorttime"
  101. },
  102. SUBMIT_FORMATS_MAP: {
  103. date: "date",
  104. time: "time",
  105. datetime: "datetime-local"
  106. },
  107. toStandardDateFormat: function(date, type) {
  108. var pattern = dateUtils.FORMATS_INFO[type].getStandardPattern();
  109. return dateSerialization.serializeDate(date, pattern)
  110. },
  111. fromStandardDateFormat: function(text) {
  112. var date = dateSerialization.dateParser(text);
  113. return isDate(date) ? date : void 0
  114. },
  115. getMaxMonthDay: function(year, month) {
  116. return new Date(year, month + 1, 0).getDate()
  117. },
  118. mergeDates: function(oldValue, newValue, format) {
  119. if (!newValue) {
  120. return newValue || null
  121. }
  122. if (!oldValue || isNaN(oldValue.getTime())) {
  123. var now = new Date(null);
  124. oldValue = new Date(now.getFullYear(), now.getMonth(), now.getDate())
  125. }
  126. var result = new Date(oldValue.valueOf());
  127. var formatInfo = dateUtils.FORMATS_INFO[format];
  128. each(formatInfo.components, function() {
  129. var componentInfo = dateUtils.DATE_COMPONENTS_INFO[this];
  130. result[componentInfo.setter](newValue[componentInfo.getter]())
  131. });
  132. return result
  133. },
  134. getLongestCaptionIndex: function(captionArray) {
  135. var longestIndex = 0;
  136. var longestCaptionLength = 0;
  137. var i;
  138. for (i = 0; i < captionArray.length; ++i) {
  139. if (captionArray[i].length > longestCaptionLength) {
  140. longestIndex = i;
  141. longestCaptionLength = captionArray[i].length
  142. }
  143. }
  144. return longestIndex
  145. },
  146. formatUsesMonthName: function(format) {
  147. return dateLocalization.formatUsesMonthName(format)
  148. },
  149. formatUsesDayName: function(format) {
  150. return dateLocalization.formatUsesDayName(format)
  151. },
  152. getLongestDate: function(format, monthNames, dayNames) {
  153. var stringFormat = getStringFormat(format);
  154. var month = 9;
  155. if (!stringFormat || dateUtils.formatUsesMonthName(stringFormat)) {
  156. month = dateUtils.getLongestCaptionIndex(monthNames)
  157. }
  158. var longestDate = new Date(1888, month, 21, 23, 59, 59, 999);
  159. if (!stringFormat || dateUtils.formatUsesDayName(stringFormat)) {
  160. var date = longestDate.getDate() - longestDate.getDay() + dateUtils.getLongestCaptionIndex(dayNames);
  161. longestDate.setDate(date)
  162. }
  163. return longestDate
  164. },
  165. normalizeTime: function(date) {
  166. date.setSeconds(0);
  167. date.setMilliseconds(0)
  168. }
  169. };
  170. dateUtils.DATE_COMPONENTS_INFO = {
  171. year: {
  172. getter: "getFullYear",
  173. setter: "setFullYear",
  174. formatter: function(value, showNames, date) {
  175. var formatDate = new Date(date.getTime());
  176. formatDate.setFullYear(value);
  177. return dateLocalization.format(formatDate, "yyyy")
  178. },
  179. startValue: void 0,
  180. endValue: void 0
  181. },
  182. day: {
  183. getter: "getDate",
  184. setter: "setDate",
  185. formatter: function(value, showNames, date) {
  186. var formatDate = new Date(date.getTime());
  187. formatDate.setDate(value);
  188. if (!showNames) {
  189. return dateLocalization.format(formatDate, "d")
  190. }
  191. return dateUtils.DATE_COMPONENT_TEXT_FORMATTER(value, dateLocalization.getDayNames()[formatDate.getDay()])
  192. },
  193. startValue: 1,
  194. endValue: void 0
  195. },
  196. month: {
  197. getter: "getMonth",
  198. setter: "setMonth",
  199. formatter: function(value, showNames) {
  200. var monthName = dateLocalization.getMonthNames()[value];
  201. return showNames ? dateUtils.DATE_COMPONENT_TEXT_FORMATTER(value + 1, monthName) : monthName
  202. },
  203. startValue: 0,
  204. endValue: 11
  205. },
  206. hours: {
  207. getter: "getHours",
  208. setter: "setHours",
  209. formatter: function(value) {
  210. return dateLocalization.format(new Date(0, 0, 0, value), "hour")
  211. },
  212. startValue: 0,
  213. endValue: 23
  214. },
  215. minutes: {
  216. getter: "getMinutes",
  217. setter: "setMinutes",
  218. formatter: function(value) {
  219. return dateLocalization.format(new Date(0, 0, 0, 0, value), "minute")
  220. },
  221. startValue: 0,
  222. endValue: 59
  223. },
  224. seconds: {
  225. getter: "getSeconds",
  226. setter: "setSeconds",
  227. formatter: function(value) {
  228. return dateLocalization.format(new Date(0, 0, 0, 0, 0, value), "second")
  229. },
  230. startValue: 0,
  231. endValue: 59
  232. },
  233. milliseconds: {
  234. getter: "getMilliseconds",
  235. setter: "setMilliseconds",
  236. formatter: function(value) {
  237. return dateLocalization.format(new Date(0, 0, 0, 0, 0, 0, value), "millisecond")
  238. },
  239. startValue: 0,
  240. endValue: 999
  241. }
  242. };
  243. module.exports = dateUtils;