{"id":290,"date":"2023-01-09T05:00:00","date_gmt":"2023-01-09T13:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=290"},"modified":"2023-01-12T09:06:41","modified_gmt":"2023-01-12T17:06:41","slug":"date-pipe-default-format-and-timezone","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/date-pipe-default-format-and-timezone\/","title":{"rendered":"Date pipe default format and timezone"},"content":{"rendered":"\n<p>The date pipe is the most convenient way to format dates with Angular. However, very often, we need to use a consistent date format throughout our application, which means that we have to pass that custom format every time we use the date pipe:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">span<\/span>&gt;<\/span>Today is {{today | date: 'MM\/dd\/yy'}}<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">span<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Of course, we could store that format in a constant and reuse that constant every time we use the pipe, but that&#8217;s not very convenient.<\/p>\n\n\n\n<p>Luckily for us, since Angular 15, we can now set a default date format (and timezone) by configuring a new injection token called <code><a href=\"https:\/\/angular.io\/api\/common\/DATE_PIPE_DEFAULT_OPTIONS\" target=\"_blank\" rel=\"noopener\" title=\"\">DATE_PIPE_DEFAULT_OPTIONS<\/a><\/code>.<\/p>\n\n\n\n<p>It works by adding the following code to your dependency injection config (array of <code>providers<\/code>) in <code>app.modules.ts<\/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\">providers: &#91;\n  {\n    <span class=\"hljs-attr\">provide<\/span>: DATE_PIPE_DEFAULT_OPTIONS, <span class=\"hljs-attr\">useValue<\/span>: {<span class=\"hljs-attr\">dateFormat<\/span>: <span class=\"hljs-string\">'MM\/dd\/yy'<\/span>}\n  }\n]<\/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>With such a config in place, we can use our pipe without any parameters and have our default formatting applied automatically in our entire application:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">span<\/span>&gt;<\/span>Today is {{today | date}}<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">span<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The timezone can be customized as well with the <code>timezone<\/code> property of that same <code><a href=\"https:\/\/angular.io\/api\/common\/DatePipeConfig\" target=\"_blank\" rel=\"noopener\" title=\"\">DatePipeConfig<\/a><\/code> object.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The date pipe is the most convenient way to format dates with Angular. However, very often, we need to use a consistent date format throughout our application, which means that we have to pass that custom format every time we use the date pipe: Of course, we could store that format in a constant and [&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,14],"tags":[],"class_list":["post-290","post","type-post","status-publish","format-standard","hentry","category-angular","category-date-formatting","category-pipes"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/290","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=290"}],"version-history":[{"count":7,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/290\/revisions"}],"predecessor-version":[{"id":328,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/290\/revisions\/328"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}