{"id":6265,"date":"2022-05-30T09:04:36","date_gmt":"2022-05-30T08:04:36","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=6265"},"modified":"2022-05-30T09:04:36","modified_gmt":"2022-05-30T08:04:36","slug":"typescript-simple-date-format","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/typescript-simple-date-format\/","title":{"rendered":"Typescript: Simple date format"},"content":{"rendered":"\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"typescript\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">const pad = (val: any, len: number) => String(val).padStart(len, \"0\");\n\nexport function formatDate (date:Date, format: string) {\n    const y =  date.getFullYear(), m =  date.getMonth(), d = date.getDate();\n    const h =  date.getHours(), min =  date.getMinutes(), s =  date.getSeconds();\n    const f =  date.getMilliseconds();\n\n    const arr: Array&lt;Array&lt;any>> = [\n        ['yyyy',  y],\n        ['yy', pad(y, 4).slice(2)],\n        ['y', pad(y, 4).slice(3)],\n        ['MM', pad(m, 2)],\n        ['M', m],\n        ['dd', pad(d, 2)],\n        ['d', d],\n        ['HH', pad(h, 2)],\n        ['H', h],\n        ['hh', pad(h, 2)],\n        ['h', h]    ,\n        ['mm', pad(min, 2)],\n        ['m', min],\n        ['ss', pad(s, 2)],\n        ['s', s],\n        ['fff', pad(f, 3)],\n        ['ff', pad(f, 3).slice(0, 2)],\n        ['f', pad(f, 3).slice(0, 1)]\n    ];\n    arr.sort((a,b) => b.length - a.length);\n\n    let str = format;\n    for (const item of arr) {\n        str = str.replace(item[0], String(item[1]));\n    }\n    return str;\n};\n\nconsole.log(formatDate(new Date(1988, 10, 14, 12, 34, 55, 123), \"dd-MM-yyyy HH:mm:ss.fff\"));\n<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6265","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/6265","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=6265"}],"version-history":[{"count":1,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/6265\/revisions"}],"predecessor-version":[{"id":6266,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/6265\/revisions\/6266"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=6265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=6265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=6265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}