{"route":"/en-US-v0.14.1/reference/foundations/datetime/","title":"Datetime","description":"Documentation for the Datetime type.","part":null,"outline":[{"id":"summary","name":"Summary","children":[]},{"id":"example","name":"Example","children":[]},{"id":"datetime-and-duration","name":"Datetime and Duration","children":[]},{"id":"format","name":"Format","children":[]},{"id":"constructor","name":"Constructor","children":[{"id":"constructor-year","name":"year","children":[]},{"id":"constructor-month","name":"month","children":[]},{"id":"constructor-day","name":"day","children":[]},{"id":"constructor-hour","name":"hour","children":[]},{"id":"constructor-minute","name":"minute","children":[]},{"id":"constructor-second","name":"second","children":[]}]},{"id":"definitions","name":"Definitions","children":[{"id":"definitions-today","name":"Today","children":[{"id":"definitions-today-offset","name":"offset","children":[]}]},{"id":"definitions-display","name":"Display","children":[{"id":"definitions-display-pattern","name":"pattern","children":[]}]},{"id":"definitions-year","name":"Year","children":[]},{"id":"definitions-month","name":"Month","children":[]},{"id":"definitions-weekday","name":"Weekday","children":[]},{"id":"definitions-day","name":"Day","children":[]},{"id":"definitions-hour","name":"Hour","children":[]},{"id":"definitions-minute","name":"Minute","children":[]},{"id":"definitions-second","name":"Second","children":[]},{"id":"definitions-ordinal","name":"Ordinal","children":[]}]}],"body":{"kind":"type","content":{"name":"datetime","title":"Datetime","keywords":[],"oneliner":"Represents a date, a time, or a combination of both.","details":"<p>Represents a date, a time, or a combination of both.</p>\n<p>Can be created by either specifying a custom datetime using this type's\nconstructor function or getting the current date with <a href=\"/en-US-v0.14.1/reference/foundations/datetime/#definitions-today\" title=\"`datetime.today`\"><code>datetime.today</code></a>.</p>\n<h2 id=\"example\">Example</h2>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> date <span class=\"typ-op\">=</span> <span class=\"typ-func\">datetime</span><span class=\"typ-punct\">(</span>\n  year<span class=\"typ-punct\">:</span> <span class=\"typ-num\">2020</span><span class=\"typ-punct\">,</span>\n  month<span class=\"typ-punct\">:</span> <span class=\"typ-num\">10</span><span class=\"typ-punct\">,</span>\n  day<span class=\"typ-punct\">:</span> <span class=\"typ-num\">4</span><span class=\"typ-punct\">,</span>\n<span class=\"typ-punct\">)</span>\n\n<span class=\"typ-pol\">#</span><span class=\"typ-pol\">date</span><span class=\"typ-punct\">.</span><span class=\"typ-func\">display</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">)</span> <span class=\"typ-escape\">\\</span>\n<span class=\"typ-pol\">#</span><span class=\"typ-pol\">date</span><span class=\"typ-punct\">.</span><span class=\"typ-func\">display</span><span class=\"typ-punct\">(</span>\n  <span class=\"typ-str\">&quot;y:[year repr:last_two]&quot;</span>\n<span class=\"typ-punct\">)</span>\n\n<span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> time <span class=\"typ-op\">=</span> <span class=\"typ-func\">datetime</span><span class=\"typ-punct\">(</span>\n  hour<span class=\"typ-punct\">:</span> <span class=\"typ-num\">18</span><span class=\"typ-punct\">,</span>\n  minute<span class=\"typ-punct\">:</span> <span class=\"typ-num\">2</span><span class=\"typ-punct\">,</span>\n  second<span class=\"typ-punct\">:</span> <span class=\"typ-num\">23</span><span class=\"typ-punct\">,</span>\n<span class=\"typ-punct\">)</span>\n\n<span class=\"typ-pol\">#</span><span class=\"typ-pol\">time</span><span class=\"typ-punct\">.</span><span class=\"typ-func\">display</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">)</span> <span class=\"typ-escape\">\\</span>\n<span class=\"typ-pol\">#</span><span class=\"typ-pol\">time</span><span class=\"typ-punct\">.</span><span class=\"typ-func\">display</span><span class=\"typ-punct\">(</span>\n  <span class=\"typ-str\">&quot;h:[hour repr:12][period]&quot;</span>\n<span class=\"typ-punct\">)</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.14.1/assets/689464aa0d75be9b3106ad0dcea028d2.png\" alt=\"Preview\"></div></div>\n<h2 id=\"datetime-and-duration\">Datetime and Duration</h2>\n<p>You can get a <a href=\"/en-US-v0.14.1/reference/foundations/duration/\" title=\"duration\">duration</a> by subtracting two datetime:</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> first-of-march <span class=\"typ-op\">=</span> <span class=\"typ-func\">datetime</span><span class=\"typ-punct\">(</span>day<span class=\"typ-punct\">:</span> <span class=\"typ-num\">1</span><span class=\"typ-punct\">,</span> month<span class=\"typ-punct\">:</span> <span class=\"typ-num\">3</span><span class=\"typ-punct\">,</span> year<span class=\"typ-punct\">:</span> <span class=\"typ-num\">2024</span><span class=\"typ-punct\">)</span>\n<span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> first-of-jan <span class=\"typ-op\">=</span> <span class=\"typ-func\">datetime</span><span class=\"typ-punct\">(</span>day<span class=\"typ-punct\">:</span> <span class=\"typ-num\">1</span><span class=\"typ-punct\">,</span> month<span class=\"typ-punct\">:</span> <span class=\"typ-num\">1</span><span class=\"typ-punct\">,</span> year<span class=\"typ-punct\">:</span> <span class=\"typ-num\">2024</span><span class=\"typ-punct\">)</span>\n<span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> distance <span class=\"typ-op\">=</span> first-of-march <span class=\"typ-op\">-</span> first-of-jan\n<span class=\"typ-pol\">#</span><span class=\"typ-pol\">distance</span><span class=\"typ-punct\">.</span><span class=\"typ-func\">hours</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">)</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.14.1/assets/c4920f9ef579222c3ca2c76488051bfc.png\" alt=\"Preview\"></div></div>\n<p>You can also add/subtract a datetime and a duration to retrieve a new,\noffset datetime:</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> date <span class=\"typ-op\">=</span> <span class=\"typ-func\">datetime</span><span class=\"typ-punct\">(</span>day<span class=\"typ-punct\">:</span> <span class=\"typ-num\">1</span><span class=\"typ-punct\">,</span> month<span class=\"typ-punct\">:</span> <span class=\"typ-num\">3</span><span class=\"typ-punct\">,</span> year<span class=\"typ-punct\">:</span> <span class=\"typ-num\">2024</span><span class=\"typ-punct\">)</span>\n<span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> two-days <span class=\"typ-op\">=</span> <span class=\"typ-func\">duration</span><span class=\"typ-punct\">(</span>days<span class=\"typ-punct\">:</span> <span class=\"typ-num\">2</span><span class=\"typ-punct\">)</span>\n<span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> two-days-earlier <span class=\"typ-op\">=</span> date <span class=\"typ-op\">-</span> two-days\n<span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> two-days-later <span class=\"typ-op\">=</span> date <span class=\"typ-op\">+</span> two-days\n\n<span class=\"typ-pol\">#</span><span class=\"typ-pol\">date</span><span class=\"typ-punct\">.</span><span class=\"typ-func\">display</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">)</span> <span class=\"typ-escape\">\\</span>\n<span class=\"typ-pol\">#</span><span class=\"typ-pol\">two-days-earlier</span><span class=\"typ-punct\">.</span><span class=\"typ-func\">display</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">)</span> <span class=\"typ-escape\">\\</span>\n<span class=\"typ-pol\">#</span><span class=\"typ-pol\">two-days-later</span><span class=\"typ-punct\">.</span><span class=\"typ-func\">display</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">)</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.14.1/assets/47e04f8fac503056ac0313359f787f8b.png\" alt=\"Preview\"></div></div>\n<h2 id=\"format\">Format</h2>\n<p>You can specify a customized formatting using the\n<a href=\"/en-US-v0.14.1/reference/foundations/datetime/#definitions-display\"><code>display</code></a> method. The format of a datetime is\nspecified by providing <em>components</em> with a specified number of <em>modifiers</em>.\nA component represents a certain part of the datetime that you want to\ndisplay, and with the help of modifiers you can define how you want to\ndisplay that component. In order to display a component, you wrap the name\nof the component in square brackets (e.g. <code>[year]</code> will display the year).\nIn order to add modifiers, you add a space after the component name followed\nby the name of the modifier, a colon and the value of the modifier (e.g.\n<code>[month repr:short]</code> will display the short representation of the month).</p>\n<p>The possible combination of components and their respective modifiers is as\nfollows:</p>\n<ul>\n<li><code>year</code>: Displays the year of the datetime.\n<ul>\n<li><code>padding</code>: Can be either <code>zero</code>, <code>space</code> or <code>none</code>. Specifies how the\nyear is padded.</li>\n<li><code>repr</code> Can be either <code>full</code> in which case the full year is displayed or\n<code>last_two</code> in which case only the last two digits are displayed.</li>\n<li><code>sign</code>: Can be either <code>automatic</code> or <code>mandatory</code>. Specifies when the\nsign should be displayed.</li>\n</ul>\n</li>\n<li><code>month</code>: Displays the month of the datetime.\n<ul>\n<li><code>padding</code>: Can be either <code>zero</code>, <code>space</code> or <code>none</code>. Specifies how the\nmonth is padded.</li>\n<li><code>repr</code>: Can be either <code>numerical</code>, <code>long</code> or <code>short</code>. Specifies if the\nmonth should be displayed as a number or a word. Unfortunately, when\nchoosing the word representation, it can currently only display the\nEnglish version. In the future, it is planned to support localization.</li>\n</ul>\n</li>\n<li><code>day</code>: Displays the day of the datetime.\n<ul>\n<li><code>padding</code>: Can be either <code>zero</code>, <code>space</code> or <code>none</code>. Specifies how the\nday is padded.</li>\n</ul>\n</li>\n<li><code>week_number</code>: Displays the week number of the datetime.\n<ul>\n<li><code>padding</code>: Can be either <code>zero</code>, <code>space</code> or <code>none</code>. Specifies how the\nweek number is padded.</li>\n<li><code>repr</code>: Can be either <code>ISO</code>, <code>sunday</code> or <code>monday</code>. In the case of <code>ISO</code>,\nweek numbers are between 1 and 53, while the other ones are between 0\nand 53.</li>\n</ul>\n</li>\n<li><code>weekday</code>: Displays the weekday of the date.\n<ul>\n<li><code>repr</code> Can be either <code>long</code>, <code>short</code>, <code>sunday</code> or <code>monday</code>. In the case\nof <code>long</code> and <code>short</code>, the corresponding English name will be displayed\n(same as for the month, other languages are currently not supported). In\nthe case of <code>sunday</code> and <code>monday</code>, the numerical value will be displayed\n(assuming Sunday and Monday as the first day of the week, respectively).</li>\n<li><code>one_indexed</code>: Can be either <code>true</code> or <code>false</code>. Defines whether the\nnumerical representation of the week starts with 0 or 1.</li>\n</ul>\n</li>\n<li><code>hour</code>: Displays the hour of the date.\n<ul>\n<li><code>padding</code>: Can be either <code>zero</code>, <code>space</code> or <code>none</code>. Specifies how the\nhour is padded.</li>\n<li><code>repr</code>: Can be either <code>24</code> or <code>12</code>. Changes whether the hour is\ndisplayed in the 24-hour or 12-hour format.</li>\n</ul>\n</li>\n<li><code>period</code>: The AM/PM part of the hour\n<ul>\n<li><code>case</code>: Can be <code>lower</code> to display it in lower case and <code>upper</code> to\ndisplay it in upper case.</li>\n</ul>\n</li>\n<li><code>minute</code>: Displays the minute of the date.\n<ul>\n<li><code>padding</code>: Can be either <code>zero</code>, <code>space</code> or <code>none</code>. Specifies how the\nminute is padded.</li>\n</ul>\n</li>\n<li><code>second</code>: Displays the second of the date.\n<ul>\n<li><code>padding</code>: Can be either <code>zero</code>, <code>space</code> or <code>none</code>. Specifies how the\nsecond is padded.</li>\n</ul>\n</li>\n</ul>\n<p>Keep in mind that not always all components can be used. For example, if you\ncreate a new datetime with <code><span class=\"typ-func\">datetime</span><span class=\"typ-punct\">(</span>year<span class=\"typ-punct\">:</span> <span class=\"typ-num\">2023</span><span class=\"typ-punct\">,</span> month<span class=\"typ-punct\">:</span> <span class=\"typ-num\">10</span><span class=\"typ-punct\">,</span> day<span class=\"typ-punct\">:</span> <span class=\"typ-num\">13</span><span class=\"typ-punct\">)</span></code>, it\nwill be stored as a plain date internally, meaning that you cannot use\ncomponents such as <code>hour</code> or <code>minute</code>, which would only work on datetimes\nthat have a specified time.</p>","constructor":{"path":[],"name":"datetime","title":"Construct","keywords":[],"oneliner":"Creates a new datetime.","element":false,"contextual":false,"deprecationMessage":null,"deprecationUntil":null,"details":[{"kind":"html","content":"<p>Creates a new datetime.</p>\n<p>You can specify the <a href=\"/en-US-v0.14.1/reference/foundations/datetime/\" title=\"datetime\">datetime</a> using a year, month, day, hour, minute,\nand second.</p>\n<p><em>Note</em>: Depending on which components of the datetime you specify, Typst\nwill store it in one of the following three ways:</p>\n<ul>\n<li>If you specify year, month and day, Typst will store just a date.</li>\n<li>If you specify hour, minute and second, Typst will store just a time.</li>\n<li>If you specify all of year, month, day, hour, minute and second, Typst\nwill store a full datetime.</li>\n</ul>\n<p>Depending on how it is stored, the <a href=\"/en-US-v0.14.1/reference/foundations/datetime/#definitions-display\"><code>display</code></a> method\nwill choose a different formatting by default.</p>"},{"kind":"example","content":{"body":"<div class=\"previewed-code\"><pre><code><span class=\"typ-func\">#</span><span class=\"typ-func\">datetime</span><span class=\"typ-punct\">(</span>\n  year<span class=\"typ-punct\">:</span> <span class=\"typ-num\">2012</span><span class=\"typ-punct\">,</span>\n  month<span class=\"typ-punct\">:</span> <span class=\"typ-num\">8</span><span class=\"typ-punct\">,</span>\n  day<span class=\"typ-punct\">:</span> <span class=\"typ-num\">3</span><span class=\"typ-punct\">,</span>\n<span class=\"typ-punct\">)</span><span class=\"typ-punct\">.</span><span class=\"typ-func\">display</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">)</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.14.1/assets/ea6a67351ca9372b235ef5ecb52a2e8b.png\" alt=\"Preview\"></div></div>","title":null}}],"self":false,"params":[{"name":"year","details":[{"kind":"html","content":"<p>The year of the datetime.</p>"}],"types":["int"],"strings":[],"default":null,"positional":false,"named":true,"required":false,"variadic":false,"settable":false},{"name":"month","details":[{"kind":"html","content":"<p>The month of the datetime.</p>"}],"types":["int"],"strings":[],"default":null,"positional":false,"named":true,"required":false,"variadic":false,"settable":false},{"name":"day","details":[{"kind":"html","content":"<p>The day of the datetime.</p>"}],"types":["int"],"strings":[],"default":null,"positional":false,"named":true,"required":false,"variadic":false,"settable":false},{"name":"hour","details":[{"kind":"html","content":"<p>The hour of the datetime.</p>"}],"types":["int"],"strings":[],"default":null,"positional":false,"named":true,"required":false,"variadic":false,"settable":false},{"name":"minute","details":[{"kind":"html","content":"<p>The minute of the datetime.</p>"}],"types":["int"],"strings":[],"default":null,"positional":false,"named":true,"required":false,"variadic":false,"settable":false},{"name":"second","details":[{"kind":"html","content":"<p>The second of the datetime.</p>"}],"types":["int"],"strings":[],"default":null,"positional":false,"named":true,"required":false,"variadic":false,"settable":false}],"returns":["datetime"],"scope":[]},"scope":[{"path":["datetime"],"name":"today","title":"Today","keywords":[],"oneliner":"Returns the current date.","element":false,"contextual":false,"deprecationMessage":null,"deprecationUntil":null,"details":[{"kind":"html","content":"<p>Returns the current date.</p>\n<p>In the CLI, this can be overridden with the <code>--creation-timestamp</code>\nargument or by setting the\n<a href=\"https://reproducible-builds.org/specs/source-date-epoch/\"><code>SOURCE_DATE_EPOCH</code></a>\nenvironment variable. In both cases, the value should be given as a UNIX\ntimestamp.</p>"},{"kind":"example","content":{"body":"<div class=\"previewed-code\"><pre><code>Today&#39;s date is\n<span class=\"typ-pol\">#</span><span class=\"typ-pol\">datetime</span><span class=\"typ-punct\">.</span><span class=\"typ-func\">today</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">)</span><span class=\"typ-punct\">.</span><span class=\"typ-func\">display</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">)</span>.\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.14.1/assets/48e483281c9fcbf61b1db93b35e8e039.png\" alt=\"Preview\"></div></div>","title":null}}],"self":false,"params":[{"name":"offset","details":[{"kind":"html","content":"<p>An offset to apply to the current UTC date. If set to <code><span class=\"typ-key\">auto</span></code>, the\noffset will be the local offset.</p>"}],"types":["auto","int"],"strings":[],"default":"<code><span class=\"typ-key\">auto</span></code>","positional":false,"named":true,"required":false,"variadic":false,"settable":false}],"returns":["datetime"],"scope":[]},{"path":["datetime"],"name":"display","title":"Display","keywords":[],"oneliner":"Displays the datetime in a specified format.","element":false,"contextual":false,"deprecationMessage":null,"deprecationUntil":null,"details":[{"kind":"html","content":"<p>Displays the datetime in a specified format.</p>\n<p>Depending on whether you have defined just a date, a time or both, the\ndefault format will be different. If you specified a date, it will be\n<code>[year]-[month]-[day]</code>. If you specified a time, it will be\n<code>[hour]:[minute]:[second]</code>. In the case of a datetime, it will be\n<code>[year]-[month]-[day] [hour]:[minute]:[second]</code>.</p>\n<p>See the <a href=\"/en-US-v0.14.1/reference/foundations/datetime/#format\">format syntax</a> for more information.</p>"}],"self":true,"params":[{"name":"pattern","details":[{"kind":"html","content":"<p>The format used to display the datetime.</p>"}],"types":["auto","str"],"strings":[],"default":"<code><span class=\"typ-key\">auto</span></code>","positional":true,"named":false,"required":false,"variadic":false,"settable":false}],"returns":["str"],"scope":[]},{"path":["datetime"],"name":"year","title":"Year","keywords":[],"oneliner":"The year if it was specified, or `{none}` for times without a date.","element":false,"contextual":false,"deprecationMessage":null,"deprecationUntil":null,"details":[{"kind":"html","content":"<p>The year if it was specified, or <code><span class=\"typ-key\">none</span></code> for times without a date.</p>"}],"self":true,"params":[],"returns":["none","int"],"scope":[]},{"path":["datetime"],"name":"month","title":"Month","keywords":[],"oneliner":"The month if it was specified, or `{none}` for times without a date.","element":false,"contextual":false,"deprecationMessage":null,"deprecationUntil":null,"details":[{"kind":"html","content":"<p>The month if it was specified, or <code><span class=\"typ-key\">none</span></code> for times without a date.</p>"}],"self":true,"params":[],"returns":["none","int"],"scope":[]},{"path":["datetime"],"name":"weekday","title":"Weekday","keywords":[],"oneliner":"The weekday (counting Monday as 1) or `{none}` for times without a date.","element":false,"contextual":false,"deprecationMessage":null,"deprecationUntil":null,"details":[{"kind":"html","content":"<p>The weekday (counting Monday as 1) or <code><span class=\"typ-key\">none</span></code> for times without a date.</p>"}],"self":true,"params":[],"returns":["none","int"],"scope":[]},{"path":["datetime"],"name":"day","title":"Day","keywords":[],"oneliner":"The day if it was specified, or `{none}` for times without a date.","element":false,"contextual":false,"deprecationMessage":null,"deprecationUntil":null,"details":[{"kind":"html","content":"<p>The day if it was specified, or <code><span class=\"typ-key\">none</span></code> for times without a date.</p>"}],"self":true,"params":[],"returns":["none","int"],"scope":[]},{"path":["datetime"],"name":"hour","title":"Hour","keywords":[],"oneliner":"The hour if it was specified, or `{none}` for dates without a time.","element":false,"contextual":false,"deprecationMessage":null,"deprecationUntil":null,"details":[{"kind":"html","content":"<p>The hour if it was specified, or <code><span class=\"typ-key\">none</span></code> for dates without a time.</p>"}],"self":true,"params":[],"returns":["none","int"],"scope":[]},{"path":["datetime"],"name":"minute","title":"Minute","keywords":[],"oneliner":"The minute if it was specified, or `{none}` for dates without a time.","element":false,"contextual":false,"deprecationMessage":null,"deprecationUntil":null,"details":[{"kind":"html","content":"<p>The minute if it was specified, or <code><span class=\"typ-key\">none</span></code> for dates without a time.</p>"}],"self":true,"params":[],"returns":["none","int"],"scope":[]},{"path":["datetime"],"name":"second","title":"Second","keywords":[],"oneliner":"The second if it was specified, or `{none}` for dates without a time.","element":false,"contextual":false,"deprecationMessage":null,"deprecationUntil":null,"details":[{"kind":"html","content":"<p>The second if it was specified, or <code><span class=\"typ-key\">none</span></code> for dates without a time.</p>"}],"self":true,"params":[],"returns":["none","int"],"scope":[]},{"path":["datetime"],"name":"ordinal","title":"Ordinal","keywords":[],"oneliner":"The ordinal (day of the year), or `{none}` for times without a date.","element":false,"contextual":false,"deprecationMessage":null,"deprecationUntil":null,"details":[{"kind":"html","content":"<p>The ordinal (day of the year), or <code><span class=\"typ-key\">none</span></code> for times without a date.</p>"}],"self":true,"params":[],"returns":["none","int"],"scope":[]}]}}}