当前位置: 首页 > news >正文

【Typst】自定义模块mDateTime

用字符串构造datetime

Typst自带的datetime需要使用字典形式构造。我觉得不太好看,所以编写函数用字符串构造datetime

实现

// 日期函数
#let day(date_str) = {let(y,m,d) = date_str.split("-")return datetime(year:int(y),month:int(m),day:int(d),)
}// 时间函数
#let time(time_str) = {let(h,m,s) = time_str.split(":")return datetime(hour:int(h),minute:int(m),second:int(s),)
}// 日期时间函数
#let day_time(date_time_str) = {let(date_str,time_str) = date_time_str.split(" ")let(y,m,d) = date_str.split("-")let(h,mt,s) = time_str.split(":")return datetime(year:int(y),month:int(m),day:int(d),hour:int(h),minute:int(mt),second:int(s),)
}

使用也很简单,调用函数即可。

#day("2025-10-11")  // datetime(year: 2025,month:10,day: 11)
#time("12:09:08")   // datetime(hour:12,minute:9,second:8)
#day_time("2025-10-12 11:08:09")

月历和年历

发现自己很想用Typst搞日历,第三方包cineca的日历不持支中文,于是搜出几年前在Godot3.5编写的日期时间函数库,实现了Typst的版本,并制作了基于表格的月历和年历函数。

// 判断是否是闰年(leap year)
#let is_leap_year(year) = {let bol = falseif calc.rem(year,4) == 0 {bol = trueif calc.rem(year,100) == 0 {  // 年份是100的倍数if calc.rem(year,400) == 0 {  // 必须是400的整数倍bol = true} else {bol = false}}}return bol
}// 返回某年的某月共有多少天	
#let get_month_day_count(year,month) = {let day_count = 0if month == 2 {if is_leap_year(year){ //闰年day_count = 29} else {day_count = 28}		} else if month in (1,3,5,7,8,10,12) { // 大月day_count = 31} else if month in (4,6,9,11){         // 小月day_count = 30}return day_count
}// 返回某年共有多少天	
#let get_year_day_count(year) = {let day_count = 0for i in range(12){day_count += get_month_day_count(year,i+1)}return day_count
}// 获取某年某月第一天星期几(数字)
#let get_first_weekday(year,month) = {let d = str(year) + "-" + str(month) + "-01"return day(d).weekday()
}// 获取某年某月第一天星期几(汉字)
#let get_first_weekday_name(year,month) = {import "@preview/a2c-nums:0.0.1": int-to-cn-numlet w = get_first_weekday(year,month)if w in range(1,7) {return int-to-cn-num(w)} else {return "日"}
}// 获取月历数据序列
#let get_month_list(year,month) = {let w = get_first_weekday(year,month)   // 本月第一天周几let m = get_month_day_count(year,month) // 本月天数let s = 42 - m // 剩余填充let list = ()// 填充前空白if w in range(1,7) {for i in range(1,w+1) {list.push([])}s -= w}// 填充日数for i in range(1,m+1) {list.push([#i])}// 填充后空白for i in range(1,s) {list.push([ \ ])}return list
}// 显示月历
#let show_month(year,month,show_year:true
) = {[#box[#align(center)[#if show_year [#year] #month]#table(columns:7,[],[],[],[],[],[],[],..get_month_list(year,month))]]
}// 显示某年的所有月份
#let show_months(year) = {align(center)[#text(36pt,font: "Microsoft Sans Serif")[#year]]for i in range(1,13) {show_month(year,i,show_year: false)}
}

你可以使用show_month显示某年某月的月历:

#show_month(2025,3)   // 显示2025年3月的月历
#show_months(2025)    // 显示2025年整年的月历

在这里插入图片描述

在这里插入图片描述

期间

两个datetime相减,或者使用duration()函数构造,可以创建一个duration,它代表时间差或持续的一段时间。

duration(seconds: int,minutes: int,hours: int,days: int,weeks: int,
) -> duration

求两个日期之间的差值

#(day("2025-6-1") - day("2025-5-1")).days()    \\ 31d
#(day("2025-6-1") - day("2025-5-1")).hours()   \\ 744h
#(day("2025-6-1") - day("2025-5-1")).seconds() \\ 2678400s

倒计时

可以用duration来创建倒计时。

完整代码

我将完整代码保存为mDateTime.typ,并在使用时用import形式导入:

#import "mDateTime.typ":*

完整代码如下:

// 日期函数
#let day(date_str) = {let(y,m,d) = date_str.split("-")return datetime(year:int(y),month:int(m),day:int(d),)
}// 时间函数
#let time(time_str) = {let(h,m,s) = time_str.split(":")return datetime(hour:int(h),minute:int(m),second:int(s),)
}// 日期时间函数
#let day_time(date_time_str) = {let(date_str,time_str) = date_time_str.split(" ")let(y,m,d) = date_str.split("-")let(h,mt,s) = time_str.split(":")return datetime(year:int(y),month:int(m),day:int(d),hour:int(h),minute:int(mt),second:int(s),)
}// 判断是否是闰年(leap year)
#let is_leap_year(year) = {let bol = falseif calc.rem(year,4) == 0 {bol = trueif calc.rem(year,100) == 0 {  // 年份是100的倍数if calc.rem(year,400) == 0 {  // 必须是400的整数倍bol = true} else {bol = false}}}return bol
}// 返回某年的某月共有多少天	
#let get_month_day_count(year,month) = {let day_count = 0if month == 2 {if is_leap_year(year){ //闰年day_count = 29} else {day_count = 28}		} else if month in (1,3,5,7,8,10,12) { // 大月day_count = 31} else if month in (4,6,9,11){         // 小月day_count = 30}return day_count
}// 返回某年共有多少天	
#let get_year_day_count(year) = {let day_count = 0for i in range(12){day_count += get_month_day_count(year,i+1)}return day_count
}// 获取某年某月第一天星期几(数字)
#let get_first_weekday(year,month) = {let d = str(year) + "-" + str(month) + "-01"return day(d).weekday()
}// 获取某年某月第一天星期几(汉字)
#let get_first_weekday_name(year,month) = {import "@preview/a2c-nums:0.0.1": int-to-cn-numlet w = get_first_weekday(year,month)if w in range(1,7) {return int-to-cn-num(w)} else {return "日"}
}// 获取月历数据序列
#let get_month_list(year,month) = {let w = get_first_weekday(year,month)   // 本月第一天周几let m = get_month_day_count(year,month) // 本月天数let s = 42 - m // 剩余填充let list = ()// 填充前空白if w in range(1,7) {for i in range(1,w+1) {list.push([])}s -= w}// 填充日数for i in range(1,m+1) {list.push([#i])}// 填充后空白for i in range(1,s) {list.push([ \ ])}return list
}// 显示月历
#let show_month(year,month,show_year:true
) = {[#box[#align(center)[#if show_year [#year] #month]#table(columns:7,[],[],[],[],[],[],[],..get_month_list(year,month))]]
}// 显示某年的所有月份
#let show_months(year) = {align(center)[#text(36pt,font: "Microsoft Sans Serif")[#year]]for i in range(1,13) {show_month(year,i,show_year: false)}
}

相关文章:

  • ESP32 004 Thonny 配置简单的轻量级MicroPython开发IDE
  • 提升iOS开发效率:通过KeyMob等工具进行全面性能分析与调试
  • .NET 的IOC框架Unity代码示例
  • 游戏引擎学习第315天:取消排序键的反向顺序
  • 2025.6.11总结
  • 中科院1区|IF6.7:基于PCA/OPLS-DA和KEGG通路分析的多组学整合,揭示沙棘-水飞蓟复方改善高脂血症的分子基础
  • 【QT】窗口详解
  • FanControl:智能散热,稳定运行
  • JS 原型与原型链详解
  • CMO增加人工智能投资:数据表明了什么
  • 广东食品安全管理员证有哪些,怎么考的呢
  • const auto 和 auto
  • 第二节:Vben Admin v5 (vben5) Python-Flask 后端开发详解(附源码)
  • MySQL基础知识(DDL、DML)
  • Docker:修改已有容器的端口映射
  • 树莓派超全系列教程文档--(63)rpicam-apps可用选项介绍之常用选项
  • 芯伯乐XBLW GT712选型及应用设计指南
  • Redis:极速缓存与数据结构存储揭秘
  • MySQL:Prepared Statement 预处理语句
  • 软考 系统架构设计师系列知识点之杂项集萃(87)
  • 金融理财网站建设方案/游戏优化大师
  • wordpress侧栏导航/青岛网站关键词排名优化
  • 怎么用文本做网站/廊坊seo整站优化
  • 做博物馆网站最重要性/青山seo排名公司
  • 自助免费建站/百度手机助手下载免费安装
  • 一般通过逸民/宁波seo网络推广产品服务