typst

How do I get the current date in Typst?


I would like to insert the current date in the title section of my document (a la LaTeX). Is it even possible to do so yet? If so, how?


Solution

  • It is possible to obtain today's date since Typst v0.5.0; simply use the datetime.today() function, which returns a datetime object containing the current day, month and year (see datetime, today for reference). Note that in Typst there isn't, currently, any way to get the current time (i.e., hours/minutes/seconds), however (just the current date).

    You can combine this with datetime's display() method to display the datetime in your desired way (see datetime, display for reference), or use the .day() / .month() / .year() datetime methods directly:

    #let today = datetime.today()
    #today.display()  // Use the default format: year-month-day
    
    #today.display("[day]/[month]/[year]")  // Specify a custom format
    
    #today.display("[month repr:long] [day], [year]")  // Or another custom format
    
    #today.day() / #today.month() / #today.year()  // You can also do this (not zero-padded)
    // The methods above return integers directly which can be used in scripting
    

    On the 12th of September of 2023, UTC (when this answer was written), the Typst code above will produce (on Typst v0.7.0, using the rendering bot on Discord):

    Output of the sample code, including four lines, from top to bottom (excluding the double quotes): "2023-09-12", "12/09/2023", "September 12, 2023" and "12 / 9 / 2023"