首页 > 代码库 > 获取当前日期

获取当前日期

这里要用到date函数的第三种形式,下面是获得当前日期

(set ‘today (date (date-value) 0 "%Y%m%d"))

(date-value) 返回的是1970年0点累计的秒数,作为date函数的第一个参数

第二个参数是偏移的分钟,0表示没有偏移,就是现在。 这个参数主要用来做时区转换

第三个参数定义日期格式,参考下表:

formatdescription
%aabbreviated weekday name according to the current locale
%Afull weekday name according to the current locale
%babbreviated month name according to the current locale
%Bfull month name according to the current locale
%cpreferred date and time representation for the current locale
%dday of the month as a decimal number (range 01–31)
%Hhour as a decimal number using a 24-hour clock (range 00–23)
%Ihour as a decimal number using a 12-hour clock (range 01–12)
%jday of the year as a decimal number (range 001–366)
%mmonth as a decimal number (range 01–12)
%Mminute as a decimal number
%peither ‘am‘ or ‘pm‘ according to the given time value or the corresponding strings for the current locale
%Ssecond as a decimal number 0–61 (60 and 61 to account for occasional leap seconds)
%Uweek number of the current year as a decimal number, starting with the first Sunday as the first day of the first week
%wday of the week as a decimal, Sunday being 0
%Wweek number of the current year as a decimal number, starting with the first Monday as the first day of the first week
%xpreferred date representation for the current locale without the time
%Xpreferred time representation for the current locale without the date
%yyear as a decimal number without a century (range 00–99)
%Yyear as a decimal number including the century
%ztime zone or name or abbreviation (same as %Z on Win32, different on Unix)
%Ztime zone or name or abbreviation (same as %z on Win32, different on Unix)
%%a literal ‘%‘ character

下面是使用了偏移参数计算昨天的日期:

(set ‘yesterday (date (date-value) (- (* 24 60)) "%Y%m%d"))

24小时乘以60分钟,结果加上负号