如何在日期中添加“th”或“rd”

时间:2013-12-20 00:12:31

标签: lua

我想要做的是在日期中添加“th”或“rd”,即Lua中的第27,第30等。

我确信最好使用循环来完成这项任务,这可能吗?

我尝试浏览/学习并为自己尝试,但我一遍又一遍地失败了。

MeasureDate MeterDate 是添加日期的。

我已粘贴下面的代码。

;----------RainMeter

[Rainmeter]
Update=#UpdateRateAmount#



;-------- Lua Script
[LuaScript]
Measure=Script
ScriptFile=#skinspath#/MyScript.lua


;---------Variables

[Variables]
@include="#skinspath#/SkinVariables.inc"

[ClockStyle]
FontFace=#Font#
FontColor=#ClockFontColor#
AntiAlias=1
FontSize=120
StringAlign=Left
StringEffect=Border
FontEffectColor=#ClockFontEffectColor#

;---- Minute Variables

[MinuteStyle]
FontFace=#Font#
FontColor=#MinuteFontColor#
AntiAlias=1
FontSize=120
StringAlign=Left
StringEffect=Border
FontEffectColor=#MinuteFontEffectColor#


[FontStyle]
FontFace=#Font#
FontColor=#FontColor#
AntiAlias=1
StringAlign=Left
StringEffect=Border
FontEffectColor=#FontEffectColor#


;--------Date


;---------Clock
[MeasureDate]
Measure=Time
Format=%d 
UpdateDivider=#UpdateDividerAmount#

[MeasureHour]
Measure=Time
Format=%#ClockMode#
UpdateDivider=#UpdateDividerAmount#

[MeasureMinute]
Measure=Time
Format=%M
UpdateDivider=#UpdateDividerAmount#

[MeasureWeekday]
Measure=Time
Format=%A
Substitute="Sunday":"SUN","Monday":"MON","Tuesday":"TUES","Wednesday":"WED","Thursday":"THURS","Friday":"FRI","Saturday":"SAT"
UpdateDivider=#UpdateDividerAmount#


[MeasureMonth]
Measure=Time
Format=%b
UpdateDivider=#UpdateDividerAmount#
StringCase=Upper
Substitute="Jan":"JAN.","Feb":"FEB.","Mar":"MAR.","Apr":"APR.","Jun":"JUN.","Jul":"JUL.","Aug":"AUG.","Sep":"SEP.","Oct":"OCT.","Nov":"NOV.","Dec":"DEC."
FontFace=Courier

[MeasureYear]
Measure=Time
Format=%Y
UpdateDivider=#UpdateDividerAmount#

[I]
Measure=Time
Format=%p
UpdateDivider=#UpdateDividerAmount#

[MeterHour]
Meter=String
MeasureName=MeasureHour
MeterStyle=ClockStyle
X=9r
Y=-9r
Text='%1'

[BetweenMinHr]
Meter=String
FontSize=45
MeterStyle=MinuteStyle
Text=:
x=200r
y=60r

[MeterMinute]
Meter=String
MeasureName=MeasureMinute
MeterStyle=MinuteStyle
X=0r
Y=-63r
Text='%1'


[MeterWeekday]
Meter=String
MeasureName=MeasureWeekday
MeterStyle=FontStyle
StringAlign=Right
FontSize=30
StringCase=Upper
X=16r
Y=153r
Text='%1'

[MeterDate]
Meter=String
MeasureName=MeasureDate
MeterStyle=FontStyle
StringAlign=Center
FontSize=120
X=70r
Y=-10r
StringEffect=BORDER
Text='%1'


[MeterMonth]
Meter=String
MeasureName=MeasureMonth
MeterStyle=FontStyle
StringAlign=Left
FontSize=40
X=-200r
Y=43r
Text='%1'

[MeterAm/Pm]
Meter=String
MeasureName=#ClockMode#
MeterStyle=FontStyle
StringAlign=Left
FontSize=35
X=310r
Y=-75r

[MeterBox]
Meter=Image
SolidColor=2,1,1,0
X=-380r
Y=-240r
H=280
W=450

SkinVariables.inc

  

[变量]

Font=Century Gothic

;---- Clock
;For a 12 hour clock mode, put 'I'
;For a 24 hour clock mode, put 'H'
ClockMode=I

FontColor=35,35,35,255
FontEffectColor=255,255,255,150
ClockFontColor=35,35,35,255
ClockFontEffectColor=242,242,242,100

MinuteFontColor=255,255,255,255
MinuteEffectColor=0,0,0,128
MinuteFontEffectColor=0,0,0,128

3 个答案:

答案 0 :(得分:5)

您可以使用以下功能轻松完成将数字更改为序数:

function ordinal_numbers(n)
  local ordinal, digit = {"st", "nd", "rd"}, string.sub(n, -1)
  if tonumber(digit) > 0 and tonumber(digit) <= 3 and string.sub(n,-2) ~= 11 and string.sub(n,-2) ~= 12 and string.sub(n,-2) ~= 13 then
    return n .. ordinal[tonumber(digit)]
  else
    return n .. "th"
  end
end

它可能会变得更好更清洁,但它适用于您的目的。

答案 1 :(得分:0)

这是一个返回给定日期数的序数的函数:

function day_ordinal(dayn)
    last_digit = dayn % 10
    if last_digit == 1 and dayn ~= 11
        then return 'st'
    elseif last_digit == 2 and dayn ~= 12
        then return 'nd'
    elseif last_digit == 3 and dayn ~= 13
        then return 'rd'
    else 
        return 'th'
    end
end

此方法不需要任何字符串提取/比较,这很好。然后你可以有一个包含os.date和'扩展'功能的函数,允许插入这些序数,如下所示:(其中%o插入日期的序数)

"Today is %A the %d%o"

执行此操作的功能如下:

function timef(datestr,date)
    datestr = string.gsub(datestr,"%%o",day_ordinal(date.day))
    return os.date(datestr,os.time(datedate))
end 

您可以使用以下内容:

date = os.date("*t",os.time())
print(timef("%A the %d%o",date))

导致:

Sunday the 22nd

答案 2 :(得分:0)

我发现这个是因为我正在寻找一种可以轻松添加序数指标的解决方案,但是我需要的东西不仅可以轻松添加到 31 ) 所以我在这里添加我的代码,以防将来有人遇到这个问题。

function GetOrdinal(number)

    -- Creating necessary locals and tables
    local ordinal = ""
    local special_ordinals = {[1] = "st", [2] = "nd", [3] = "rd", [11] = "th", [12] = "th", [13] = "th"}
    local last_two = tonumber(string.sub(number, string.len(number) - 1))
    local last_one = tonumber(string.sub(number, string.len(number)))

    -- If last two / one digit(s) are / aren't on table
    if special_ordinals[last_two] then
        ordinal = special_ordinals[last_two]
    elseif special_ordinals[last_one] then
        ordinal = special_ordinals[last_one]
    else
        ordinal = "th"
    end

    -- Sending data back
    ordinal = number..ordinal
    return ordinal
end