【问题标题】:Convert unix time to month number?将unix时间转换为月数?
【发布时间】:2016-02-13 11:56:14
【问题描述】:

使用os.time我怎样才能得到自unix时代以来已经过去了多少个月(Unix时间戳)

我只需要它作为月份ID,所以任何类型的数字都可以,只要它每个月都在变化,并且可以反转得到实际月份。

【问题讨论】:

    标签: date unix lua unix-timestamp roblox


    【解决方案1】:
    local function GetMonth(seconds)
        local dayduration,year = 3600*24
        local days={31,0,31,30,31,30,31,31,30,31,30,31}
        for i=1970,10000 do -- For some reason too lazy to use while
            local yeardays = i%4 == 0 and i%100 ~= 0 and 366 or 365
            local yearduration = dayduration * yeardays
            if yearduration < seconds then
                seconds = seconds - yearduration
            else
                year = i break
            end
        end
        days[2]=(year%4==0) and 29 or 28
        seconds = seconds%(365*24*3600)
        for i=1,12 do
            if seconds>days[i]*dayduration then
                seconds=seconds-days[i]*dayduration
            else
                return --i + year*12  <-- If you want a unique ID
            end
        end
    end
    

    目前,它会给出数字 2,因为它是 2 月。如果您取消注释最后的唯一 ID 代码,您将得到 554,这意味着我们目前处于纪元以来的第 554 个月。

    正如 Jean-Baptiste Yunès 在他的回答 cmets 中所说,我不确定你的句子是否:

    注意:这是用于 Lua,但我无法使用 os.date

    意味着您没有 os.date,或者您不知道如何使用它。你对这两种情况都有一个答案,你可以使用你需要的那个。

    【讨论】:

    • 完美运行!是的,我的意思是我不能使用 os.date
    • 您可能需要查看“数据和时间模块”的免费模型,因为有几个模型可以满足您的所有需求。
    【解决方案2】:

    这可能会奏效:

    print (os.date("*t",os.time())["month"])
    

    os.time() 以数字形式为您提供当前日期。 os.date("*t",...) 将其转换为表格,其中month 等于日期对应的月份数。

    【讨论】:

    • 他明确表示无法使用os.date(不知道为什么,可能是ROBLOX之类的Lua引擎)
    • 如果认为他不明白 os.date() 的工作原理。不要这么粗鲁,让他决定……
    • 啊,我以为他的意思是“没有 os.date”而不是“我只是不知道如何使用它”
    • 是的,它是 ROBLOX - 没有 os.date @EinsteinK
    • 你应该使用os.time然后
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-07
    • 1970-01-01
    • 2023-03-18
    • 2012-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多