{"id":9804,"date":"2025-10-21T07:56:24","date_gmt":"2025-10-21T06:56:24","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=9804"},"modified":"2025-10-30T16:31:51","modified_gmt":"2025-10-30T15:31:51","slug":"typescript-date-functions","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/typescript-date-functions\/","title":{"rendered":"Typescript Date functions (datehelpers)"},"content":{"rendered":"\n<p>dateUtils.ts<\/p>\n\n\n\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\">export function getDateFromInput(n?: any): Date | undefined {\n    if (!n) return undefined\n    const d = new Date(n);\n    if (!d || !Number.isFinite(d?.getTime())) return undefined;\n    return d;\n}\n\nexport function dateToInt(date: Date): number | undefined {\n  if (!(date instanceof Date) || isNaN(date.getTime())) {\n    return undefined; \/\/ Ongeldige datum\n  }\n\n  const year = date.getFullYear();\n  const month = date.getMonth() + 1;\n  const day = date.getDate();\n\n  return year * 10000 + month * 100 + day;\n}\n\nexport function intToDate(value: number): Date | undefined {\n  if (!Number.isInteger(value) || value &lt; 10000101 || value > 99991231) {\n    return undefined;\n  }\n\n  const year = Math.floor(value \/ 10000);\n  const month = Math.floor((value % 10000) \/ 100);\n  const day = value % 100;\n\n  if (month &lt; 1 || month > 12 || day &lt; 1 || day > 31) {\n    return undefined;\n  }\n\n  const date = new Date(year, month - 1, day);\n\n  if (\n    date.getFullYear() !== year ||\n    date.getMonth() + 1 !== month ||\n    date.getDate() !== day\n  ) {\n    return undefined;\n  }\n\n  return date;\n}<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>dateUtils.ts<\/p>\n","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-9804","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/9804","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=9804"}],"version-history":[{"count":6,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/9804\/revisions"}],"predecessor-version":[{"id":9856,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/9804\/revisions\/9856"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=9804"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=9804"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=9804"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}