{"id":3304,"date":"2014-03-06T21:20:34","date_gmt":"2014-03-07T03:20:34","guid":{"rendered":"http:\/\/bililite.com\/blog\/?p=3304"},"modified":"2015-02-25T12:18:40","modified_gmt":"2015-02-25T18:18:40","slug":"flexcal-with-european-date-formatting","status":"publish","type":"post","link":"https:\/\/bililite.com\/blog\/2014\/03\/06\/flexcal-with-european-date-formatting\/","title":{"rendered":"<code>flexcal<\/code> with European date formatting"},"content":{"rendered":"<p>The question came up about using \"European\" dates (day\/month\/year) rather than \"American\" dates (month\/day\/year) in <a href=\"http:\/\/bililite.com\/blog\/2009\/04\/03\/new-jquery-widget-flexcal\/\" title=\"New jQuery Widget: flexcal\"><code>flexcal<\/code><\/a>. The biggest problem is that the built-in <code class=\"language-javascript\" >Date.parse()<\/code> (which is used by <code class=\"language-javascript\" >new Date(string)<\/code>) uses the American format, at least with my browsers and settings.<\/p>\r\n<p>The code in <code>flexcal<\/code> that does the formatting are in the following methods:<\/p>\r\n<dl>\r\n<dt><code>format({Date})<\/code><\/dt>\r\n<dd>Converts a <code class=\"language-javascript\" >Date<\/code> into a <code class=\"language-javascript\" >String<\/code>. Used for <em>external<\/em> formatting: it determines what string is put in the input element after the user selects a date.<\/dd>\r\n<dt><code>_date2string({Date})<\/code><\/dt>\r\n<dd>Converts a <code class=\"language-javascript\" >Date<\/code> into a <code class=\"language-javascript\" >String<\/code>. Used for <em>internal<\/em> formatting: the <code>rel<\/code> attribute of each day link in the calendar is set with this.<\/dd>\r\n<dt><code>_createDate(d, oldd)<\/code><\/dt>\r\n<dd>Attempts to coerce <code class=\"language-javascript\" >d<\/code> into a <code class=\"language-javascript\" >Date<\/code>. If it is a valid <code class=\"language-javascript\" >Date<\/code>, just returns that. Otherwise returns <code class=\"language-javascript\" >new Date(d)<\/code>. If that does not create a valid <code class=\"language-javascript\" >Date<\/code>, returns <code class=\"language-javascript\" >oldd<\/code>.<\/dd>\r\n<dd><code class=\"language-javascript\" >this._createDate(this._date2string(d))<\/code> must equal <code class=\"language-javascript\" >d<\/code> in order for the calendar to work, and <code class=\"language-javascript\" >this._createDate(this.format(d))<\/code> must equal <code class=\"language-javascript\" >d<\/code> for the calendar to be able to parse the string in the input element.<\/dd>\r\n<\/dl>\r\n\r\n<p>So to use European dates, we have to replace each of those methods. I'll use the \"dd.mm.yyyy\" format.<\/p>\r\n<script src=\"\/inc\/jquery.ui.subclass.js\"><\/script>\r\n<script src=\"\/inc\/jquery.textpopup.js\"><\/script>\r\n<script src=\"\/inc\/jquery.flexcal.js\"><\/script>\r\n<script src=\"\/inc\/jquery.scrollintoview.js\"><\/script>\r\n<script>\r\n  $('<link rel=stylesheet href=\"\/inc\/flexcal.css\" \/>').appendTo('head');\r\n<\/script>\r\n<pre><code class=\"language-html demo\">&lt;input id=&quot;eurodate&quot; value=\"01.02.2014\" \/&gt; Should start as February first, not January second.<\/code><\/pre>\r\n<pre><code class=\"language-javascript demo\">\r\nfunction euroformat (d){\r\n  return d.getDate()+\".\"+(d.getMonth()+1)+\".\"+d.getFullYear();\r\n}\r\n$.widget('bililite.euroflexcal', $.bililite.flexcal, {\r\n  format: euroformat,\r\n  _date2string: euroformat,\r\n  _createDate: function (d, oldd){\r\n    \/\/ converts d into a valid date\r\n    oldd = oldd || new Date;\r\n    if (!d) return oldd;\r\n    if (typeof d == 'string' && d.match(\/(\\d+)\\.(\\d+)\\.(\\d+)\/)){\r\n      d = RegExp.$2 + '\/' + RegExp.$1 + '\/' + RegExp.$3; \/\/ convert to American style\r\n    }\r\n    if (!(d instanceof Date)) d = new Date(d);\r\n    if (isNaN(d.getTime())) return oldd;\r\n    return d;\r\n  }\r\n});\r\n$('#eurodate').euroflexcal();\r\n<\/code><\/pre>","protected":false},"excerpt":{"rendered":"The question came up about using \"European\" dates (day\/month\/year) rather than \"American\" dates (month\/day\/year) in flexcal. The biggest problem is that the built-in Date.parse() (which is used by new Date(string)) uses the American format, at least with my browsers and settings. The code in flexcal that does the formatting are in the following methods: format({Date}) [&hellip;]","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,5],"tags":[],"_links":{"self":[{"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/posts\/3304"}],"collection":[{"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/comments?post=3304"}],"version-history":[{"count":14,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/posts\/3304\/revisions"}],"predecessor-version":[{"id":3515,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/posts\/3304\/revisions\/3515"}],"wp:attachment":[{"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/media?parent=3304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/categories?post=3304"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/tags?post=3304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}