pub enum Datetime {
    Date(Date),
    Time(Time),
    Datetime(PrimitiveDateTime),
}Expand description
Represents a date, a time, or a combination of both.
Can be created by either specifying a custom datetime using this type’s
constructor function or getting the current date with
datetime.today.
§Example
#let date = datetime(
  year: 2020,
  month: 10,
  day: 4,
)
#date.display() \
#date.display(
  "y:[year repr:last_two]"
)
#let time = datetime(
  hour: 18,
  minute: 2,
  second: 23,
)
#time.display() \
#time.display(
  "h:[hour repr:12][period]"
)§Datetime and Duration
You can get a [duration] by subtracting two datetime:
#let first-of-march = datetime(day: 1, month: 3, year: 2024)
#let first-of-jan = datetime(day: 1, month: 1, year: 2024)
#let distance = first-of-march - first-of-jan
#distance.hours()You can also add/subtract a datetime and a duration to retrieve a new, offset datetime:
#let date = datetime(day: 1, month: 3, year: 2024)
#let two-days = duration(days: 2)
#let two-days-earlier = date - two-days
#let two-days-later = date + two-days
#date.display() \
#two-days-earlier.display() \
#two-days-later.display()§Format
You can specify a customized formatting using the
display method. The format of a datetime is
specified by providing components with a specified number of modifiers.
A component represents a certain part of the datetime that you want to
display, and with the help of modifiers you can define how you want to
display that component. In order to display a component, you wrap the name
of the component in square brackets (e.g. [[year]] will display the year).
In order to add modifiers, you add a space after the component name followed
by the name of the modifier, a colon and the value of the modifier (e.g.
[[month repr:short]] will display the short representation of the month).
The possible combination of components and their respective modifiers is as follows:
- year: Displays the year of the datetime.- padding: Can be either- zero,- spaceor- none. Specifies how the year is padded.
- reprCan be either- fullin which case the full year is displayed or- last_twoin which case only the last two digits are displayed.
- sign: Can be either- automaticor- mandatory. Specifies when the sign should be displayed.
 
- month: Displays the month of the datetime.- padding: Can be either- zero,- spaceor- none. Specifies how the month is padded.
- repr: Can be either- numerical,- longor- short. Specifies if the month should be displayed as a number or a word. Unfortunately, when choosing the word representation, it can currently only display the English version. In the future, it is planned to support localization.
 
- day: Displays the day of the datetime.- padding: Can be either- zero,- spaceor- none. Specifies how the day is padded.
 
- week_number: Displays the week number of the datetime.- padding: Can be either- zero,- spaceor- none. Specifies how the week number is padded.
- repr: Can be either- ISO,- sundayor- monday. In the case of- ISO, week numbers are between 1 and 53, while the other ones are between 0 and 53.
 
- weekday: Displays the weekday of the date.- reprCan be either- long,- short,- sundayor- monday. In the case of- longand- short, the corresponding English name will be displayed (same as for the month, other languages are currently not supported). In the case of- sundayand- monday, the numerical value will be displayed (assuming Sunday and Monday as the first day of the week, respectively).
- one_indexed: Can be either- trueor- false. Defines whether the numerical representation of the week starts with 0 or 1.
 
- hour: Displays the hour of the date.- padding: Can be either- zero,- spaceor- none. Specifies how the hour is padded.
- repr: Can be either- 24or- 12. Changes whether the hour is displayed in the 24-hour or 12-hour format.
 
- period: The AM/PM part of the hour- case: Can be- lowerto display it in lower case and- upperto display it in upper case.
 
- minute: Displays the minute of the date.- padding: Can be either- zero,- spaceor- none. Specifies how the minute is padded.
 
- second: Displays the second of the date.- padding: Can be either- zero,- spaceor- none. Specifies how the second is padded.
 
Keep in mind that not always all components can be used. For example, if you
create a new datetime with {datetime(year: 2023, month: 10, day: 13)}, it
will be stored as a plain date internally, meaning that you cannot use
components such as hour or minute, which would only work on datetimes
that have a specified time.
Variants§
Date(Date)
Representation as a date.
Time(Time)
Representation as a time.
Datetime(PrimitiveDateTime)
Representation as a combination of date and time.
Implementations§
Source§impl Datetime
 
impl Datetime
Sourcepub fn from_ymd(year: i32, month: u8, day: u8) -> Option<Self>
 
pub fn from_ymd(year: i32, month: u8, day: u8) -> Option<Self>
Create a datetime from year, month, and day.
Sourcepub fn from_hms(hour: u8, minute: u8, second: u8) -> Option<Self>
 
pub fn from_hms(hour: u8, minute: u8, second: u8) -> Option<Self>
Create a datetime from hour, minute, and second.
Sourcepub fn from_ymd_hms(
    year: i32,
    month: u8,
    day: u8,
    hour: u8,
    minute: u8,
    second: u8,
) -> Option<Self>
 
pub fn from_ymd_hms( year: i32, month: u8, day: u8, hour: u8, minute: u8, second: u8, ) -> Option<Self>
Create a datetime from day and time.
Sourcepub fn from_toml_dict(dict: &Dict) -> Option<Self>
 
pub fn from_toml_dict(dict: &Dict) -> Option<Self>
Try to parse a dictionary as a TOML date.
Source§impl Datetime
 
impl Datetime
Sourcepub fn construct(
    year: Option<i32>,
    month: Option<Month>,
    day: Option<u8>,
    hour: Option<u8>,
    minute: Option<u8>,
    second: Option<u8>,
) -> StrResult<Datetime>
 
pub fn construct( year: Option<i32>, month: Option<Month>, day: Option<u8>, hour: Option<u8>, minute: Option<u8>, second: Option<u8>, ) -> StrResult<Datetime>
Creates a new datetime.
You can specify the [datetime] using a year, month, day, hour, minute, and second.
Note: Depending on which components of the datetime you specify, Typst will store it in one of the following three ways:
- If you specify year, month and day, Typst will store just a date.
- If you specify hour, minute and second, Typst will store just a time.
- If you specify all of year, month, day, hour, minute and second, Typst will store a full datetime.
Depending on how it is stored, the display method
will choose a different formatting by default.
#datetime(
  year: 2012,
  month: 8,
  day: 3,
).display()Sourcepub fn today(engine: &mut Engine<'_>, offset: Smart<i64>) -> StrResult<Datetime>
 
pub fn today(engine: &mut Engine<'_>, offset: Smart<i64>) -> StrResult<Datetime>
Returns the current date.
Today's date is
#datetime.today().display().Sourcepub fn display(&self, pattern: Smart<DisplayPattern>) -> StrResult<EcoString>
 
pub fn display(&self, pattern: Smart<DisplayPattern>) -> StrResult<EcoString>
Displays the datetime in a specified format.
Depending on whether you have defined just a date, a time or both, the
default format will be different. If you specified a date, it will be
[[year]-[month]-[day]]. If you specified a time, it will be
[[hour]:[minute]:[second]]. In the case of a datetime, it will be
[[year]-[month]-[day] [hour]:[minute]:[second]].
See the format syntax for more information.
Sourcepub fn year(&self) -> Option<i32>
 
pub fn year(&self) -> Option<i32>
The year if it was specified, or {none} for times without a date.
Sourcepub fn month(&self) -> Option<u8>
 
pub fn month(&self) -> Option<u8>
The month if it was specified, or {none} for times without a date.
Sourcepub fn weekday(&self) -> Option<u8>
 
pub fn weekday(&self) -> Option<u8>
The weekday (counting Monday as 1) or {none} for times without a date.
Sourcepub fn day(&self) -> Option<u8>
 
pub fn day(&self) -> Option<u8>
The day if it was specified, or {none} for times without a date.
Sourcepub fn hour(&self) -> Option<u8>
 
pub fn hour(&self) -> Option<u8>
The hour if it was specified, or {none} for dates without a time.
Sourcepub fn minute(&self) -> Option<u8>
 
pub fn minute(&self) -> Option<u8>
The minute if it was specified, or {none} for dates without a time.
Trait Implementations§
Source§impl FromValue for Datetime
 
impl FromValue for Datetime
Source§fn from_value(value: Value) -> HintedStrResult<Self>
 
fn from_value(value: Value) -> HintedStrResult<Self>
Self.Source§impl NativeScope for Datetime
 
impl NativeScope for Datetime
Source§fn constructor() -> Option<&'static NativeFuncData>
 
fn constructor() -> Option<&'static NativeFuncData>
Source§impl NativeType for Datetime
 
impl NativeType for Datetime
Source§impl PartialOrd for Datetime
 
impl PartialOrd for Datetime
impl Copy for Datetime
impl StructuralPartialEq for Datetime
Auto Trait Implementations§
impl Freeze for Datetime
impl RefUnwindSafe for Datetime
impl Send for Datetime
impl Sync for Datetime
impl Unpin for Datetime
impl UnwindSafe for Datetime
Blanket Implementations§
Source§impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for Swhere
    T: Real + Zero + Arithmetics + Clone,
    Swp: WhitePoint<T>,
    Dwp: WhitePoint<T>,
    D: AdaptFrom<S, Swp, Dwp, T>,
 
impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for Swhere
    T: Real + Zero + Arithmetics + Clone,
    Swp: WhitePoint<T>,
    Dwp: WhitePoint<T>,
    D: AdaptFrom<S, Swp, Dwp, T>,
Source§fn adapt_into_using<M>(self, method: M) -> Dwhere
    M: TransformMatrix<T>,
 
fn adapt_into_using<M>(self, method: M) -> Dwhere
    M: TransformMatrix<T>,
Source§fn adapt_into(self) -> D
 
fn adapt_into(self) -> D
Source§impl<T, C> ArraysFrom<C> for Twhere
    C: IntoArrays<T>,
 
impl<T, C> ArraysFrom<C> for Twhere
    C: IntoArrays<T>,
Source§fn arrays_from(colors: C) -> T
 
fn arrays_from(colors: C) -> T
Source§impl<T, C> ArraysInto<C> for Twhere
    C: FromArrays<T>,
 
impl<T, C> ArraysInto<C> for Twhere
    C: FromArrays<T>,
Source§fn arrays_into(self) -> C
 
fn arrays_into(self) -> C
Source§impl<T> BorrowMut<T> for Twhere
    T: ?Sized,
 
impl<T> BorrowMut<T> for Twhere
    T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
 
fn borrow_mut(&mut self) -> &mut T
Source§impl<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for Uwhere
    T: FromCam16Unclamped<WpParam, U>,
 
impl<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for Uwhere
    T: FromCam16Unclamped<WpParam, U>,
Source§type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar
 
type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar
parameters when converting.Source§fn cam16_into_unclamped(
    self,
    parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>,
) -> T
 
fn cam16_into_unclamped( self, parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>, ) -> T
self into C, using the provided parameters.Source§impl<T> CheckedAs for T
 
impl<T> CheckedAs for T
Source§fn checked_as<Dst>(self) -> Option<Dst>where
    T: CheckedCast<Dst>,
 
fn checked_as<Dst>(self) -> Option<Dst>where
    T: CheckedCast<Dst>,
Source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
    Src: CheckedCast<Dst>,
 
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
    Src: CheckedCast<Dst>,
Source§fn checked_cast_from(src: Src) -> Option<Dst>
 
fn checked_cast_from(src: Src) -> Option<Dst>
Source§impl<T> CloneToUninit for Twhere
    T: Clone,
 
impl<T> CloneToUninit for Twhere
    T: Clone,
Source§impl<T, C> ComponentsFrom<C> for Twhere
    C: IntoComponents<T>,
 
impl<T, C> ComponentsFrom<C> for Twhere
    C: IntoComponents<T>,
Source§fn components_from(colors: C) -> T
 
fn components_from(colors: C) -> T
Source§impl<T> Downcast for Twhere
    T: Any,
 
impl<T> Downcast for Twhere
    T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
 
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
 
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
 
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
 
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSync for T
 
impl<T> DowncastSync for T
Source§impl<T> FromAngle<T> for T
 
impl<T> FromAngle<T> for T
Source§fn from_angle(angle: T) -> T
 
fn from_angle(angle: T) -> T
angle.Source§impl<T, U> FromStimulus<U> for Twhere
    U: IntoStimulus<T>,
 
impl<T, U> FromStimulus<U> for Twhere
    U: IntoStimulus<T>,
Source§fn from_stimulus(other: U) -> T
 
fn from_stimulus(other: U) -> T
other into Self, while performing the appropriate scaling,
rounding and clamping.Source§impl<T> FromValue<Spanned<Value>> for Twhere
    T: FromValue,
 
impl<T> FromValue<Spanned<Value>> for Twhere
    T: FromValue,
Source§fn from_value(value: Spanned<Value>) -> Result<T, HintedString>
 
fn from_value(value: Spanned<Value>) -> Result<T, HintedString>
Self.Source§impl<T, U> IntoAngle<U> for Twhere
    U: FromAngle<T>,
 
impl<T, U> IntoAngle<U> for Twhere
    U: FromAngle<T>,
Source§fn into_angle(self) -> U
 
fn into_angle(self) -> U
T.Source§impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for Uwhere
    T: Cam16FromUnclamped<WpParam, U>,
 
impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for Uwhere
    T: Cam16FromUnclamped<WpParam, U>,
Source§type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar
 
type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar
parameters when converting.Source§fn into_cam16_unclamped(
    self,
    parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>,
) -> T
 
fn into_cam16_unclamped( self, parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>, ) -> T
self into C, using the provided parameters.Source§impl<T, U> IntoColor<U> for Twhere
    U: FromColor<T>,
 
impl<T, U> IntoColor<U> for Twhere
    U: FromColor<T>,
Source§fn into_color(self) -> U
 
fn into_color(self) -> U
Source§impl<T, U> IntoColorUnclamped<U> for Twhere
    U: FromColorUnclamped<T>,
 
impl<T, U> IntoColorUnclamped<U> for Twhere
    U: FromColorUnclamped<T>,
Source§fn into_color_unclamped(self) -> U
 
fn into_color_unclamped(self) -> U
Source§impl<T> IntoEither for T
 
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
 
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
 
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoResult for Twhere
    T: IntoValue,
 
impl<T> IntoResult for Twhere
    T: IntoValue,
Source§fn into_result(self, _: Span) -> Result<Value, EcoVec<SourceDiagnostic>>
 
fn into_result(self, _: Span) -> Result<Value, EcoVec<SourceDiagnostic>>
Source§impl<T> IntoStimulus<T> for T
 
impl<T> IntoStimulus<T> for T
Source§fn into_stimulus(self) -> T
 
fn into_stimulus(self) -> T
self into T, while performing the appropriate scaling,
rounding and clamping.Source§impl<T> OverflowingAs for T
 
impl<T> OverflowingAs for T
Source§fn overflowing_as<Dst>(self) -> (Dst, bool)where
    T: OverflowingCast<Dst>,
 
fn overflowing_as<Dst>(self) -> (Dst, bool)where
    T: OverflowingCast<Dst>,
Source§impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
    Src: OverflowingCast<Dst>,
 
impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
    Src: OverflowingCast<Dst>,
Source§fn overflowing_cast_from(src: Src) -> (Dst, bool)
 
fn overflowing_cast_from(src: Src) -> (Dst, bool)
Source§impl<T> Pointable for T
 
impl<T> Pointable for T
Source§impl<T> SaturatingAs for T
 
impl<T> SaturatingAs for T
Source§fn saturating_as<Dst>(self) -> Dstwhere
    T: SaturatingCast<Dst>,
 
fn saturating_as<Dst>(self) -> Dstwhere
    T: SaturatingCast<Dst>,
Source§impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
    Src: SaturatingCast<Dst>,
 
impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
    Src: SaturatingCast<Dst>,
Source§fn saturating_cast_from(src: Src) -> Dst
 
fn saturating_cast_from(src: Src) -> Dst
Source§impl<T, C> TryComponentsInto<C> for Twhere
    C: TryFromComponents<T>,
 
impl<T, C> TryComponentsInto<C> for Twhere
    C: TryFromComponents<T>,
Source§type Error = <C as TryFromComponents<T>>::Error
 
type Error = <C as TryFromComponents<T>>::Error
try_into_colors fails to cast.Source§fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>
 
fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>
Source§impl<T, U> TryIntoColor<U> for Twhere
    U: TryFromColor<T>,
 
impl<T, U> TryIntoColor<U> for Twhere
    U: TryFromColor<T>,
Source§fn try_into_color(self) -> Result<U, OutOfBounds<U>>
 
fn try_into_color(self) -> Result<U, OutOfBounds<U>>
OutOfBounds error is returned which contains
the unclamped color. Read more