{"id":297,"date":"2023-01-10T05:00:00","date_gmt":"2023-01-10T13:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=297"},"modified":"2023-01-10T09:20:34","modified_gmt":"2023-01-10T17:20:34","slug":"formatting-functions-in-angular","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/formatting-functions-in-angular\/","title":{"rendered":"Formatting functions in Angular"},"content":{"rendered":"\n<p>We mentioned the <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/date-pipe-default-format-and-timezone\/\" target=\"_blank\" rel=\"noopener\" title=\"\">default config options for the <code>date<\/code> pipe<\/a> in Angular yesterday.<\/p>\n\n\n\n<p>Did you know we can also format dates in our Typescript code using a <code><a href=\"https:\/\/angular.io\/api\/common\/formatDate\" target=\"_blank\" rel=\"noopener\" title=\"\">formatDate<\/a><\/code> function?<\/p>\n\n\n\n<p>This function has the same signature as the date pipe, which makes perfect sense because&#8230; That&#8217;s the function used by the pipe itself (<a href=\"https:\/\/github.com\/angular\/angular\/blob\/15.0.4\/packages\/common\/src\/pipes\/date_pipe.ts#L263\" target=\"_blank\" rel=\"noopener\" title=\"\">source code here<\/a>):<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">formatDate(value: string | number | <span class=\"hljs-built_in\">Date<\/span>, \n           <span class=\"hljs-attr\">format<\/span>: string, \n           <span class=\"hljs-attr\">locale<\/span>: string, \n           timezone?: string): string<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>All you need to use that function is to import it from <code>@angular\/common<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">import<\/span> {formatDate} <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\"@angular\/common\"<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The only downside of that function is that there is no default format or locale, so we have to pass those two values as parameters, which isn&#8217;t the case for the <code>date<\/code> pipe.<\/p>\n\n\n\n<p>And by the way, <a href=\"https:\/\/angular.io\/api\/common#functions\" target=\"_blank\" rel=\"noopener\" title=\"\">similar functions<\/a> are available for numbers, currencies, and percentages:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"901\" height=\"408\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/01\/image-2.png\" alt=\"\" class=\"wp-image-301\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/01\/image-2.png 901w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/01\/image-2-300x136.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/01\/image-2-768x348.png 768w\" sizes=\"auto, (max-width: 901px) 100vw, 901px\" \/><\/figure>\n\n\n\n<p>For more information on those functions, I have this tutorial on <a href=\"https:\/\/blog.angulartraining.com\/how-to-format-dates-and-numbers-with-angular-e39d3cf224be\" target=\"_blank\" rel=\"noopener\" title=\"\">formatting dates and numbers with Angular<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We mentioned the default config options for the date pipe in Angular yesterday. Did you know we can also format dates in our Typescript code using a formatDate function? This function has the same signature as the date pipe, which makes perfect sense because&#8230; That&#8217;s the function used by the pipe itself (source code here): [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,16],"tags":[],"class_list":["post-297","post","type-post","status-publish","format-standard","hentry","category-angular","category-date-formatting"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/297","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/comments?post=297"}],"version-history":[{"count":6,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/297\/revisions"}],"predecessor-version":[{"id":315,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/297\/revisions\/315"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=297"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=297"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=297"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}