Add datetime duration example in datetime document #3178 (#3931)

Co-authored-by: Laurenz <laurmaedje@gmail.com>
This commit is contained in:
unclebean 2024-04-16 20:05:07 +08:00 committed by GitHub
parent 5415095ef3
commit 4c99d6c8b3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -45,6 +45,28 @@ use crate::World;
/// )
/// ```
///
/// # Datetime and Duration
/// You can get a [duration] by subtracting two datetime:
/// ```example
/// #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:
/// ```example
/// #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`]($datetime.display) method. The format of a datetime is