本文共 4516 字,大约阅读时间需要 15 分钟。
2.os.datalocal tab=os.date("*t",os.time()) print(tab.year)print(tab.month)print(tab.day)print(tab.yday)print(tab.wday)2020Table.lua:212Table.lua:328Table.lua:4363Table.lua:52print(os.date("%m",os.time())) --12
3.
print(os.date("%Y年%m月%d日",os.time())) --2020年12月28日
local tab=os.date("*t")print(os.date("%Y/%m/%d",os.time(tab))) --2020/12/28tab.day=tab.day+2print(os.date("%Y/%m/%d",os.time(tab))) --2020/12/30
5.
local rapidjson = require "rapidjson"local ClientQuestCmdScript = CS.ClientQuestCmdScript--Lua事件通知function DataLuaNotify(key, msg) local luadata = QKDataSubjectManager.GetData("DataLua") luadata:Notify(key,msg)end--发送消息到服务器SendMsg = function(msg) if msg == nil or string.len(msg) == 0 then return end ClientQuestCmdScript.Instance:SendQuestClientData(msg)end--将Json字符串转成Tablefunction JsonToTable(jsonStr) local ok, res = pcall(rapidjson.decode,jsonStr) if ok then return res end return jsonStrendfunction TableToJson(tbl) local ok, res = pcall(rapidjson.encode,tbl) if ok then return res end return tblend---不显示小数点后面的0function FormatNum (num) if num <= 0 then return 0 else local t1, t2 = math.modf(num) ---小数如果为0,则去掉 if t2 > 0 then return num else return t1 end endend--- nNum 源数字--- n 小数位数function GetPreciseDecimal(nNum, n) if type(nNum) ~= "number" then return nNum; end n = n or 0; n = math.floor(n) if n < 0 then n = 0; end local nDecimal = 10 ^ n local nTemp = math.floor(nNum * nDecimal); local nRet = nTemp / nDecimal; return nRet;end--获取一个区间的随机数function GetRandom(n, m) local strseed = tostring(math.ceil(Time.realtimeSinceStartup * 10000)):reverse():sub(1, 7) -- print("seed=" .. strseed) -- if string.len(strseed) > 7 then -- strseed = string.sub(strseed,1, 7) -- end -- math.randomseed(tonumber(tostring(os.time()):reverse():sub(1,7))) math.randomseed(tonumber(strseed)) if not n or not m then return tonumber(string.format("%.1f",math.random())) end return math.random(n, m)end--长秒格式化(最大单位天)function FormatSecond(longsec, tag) local tag_tag = { "天", "小时", "分", "秒"} if tag == nil then tag_tag = { ":", ":", ":", ":"} end s = longsec % 60 m = (longsec - s) / 60 % 60 h = ((longsec - s) / 60 - m) / 60 % 24 d = (((longsec - s) / 60 - m) / 60 - h) / 24 local time_str = "" if d ~= 0 then time_str = string.format("%02d", d) .. tag_tag[1] end if h ~= 0 then time_str = time_str .. string.format("%02d", h) .. tag_tag[2] end if m ~= 0 then time_str = time_str .. string.format("%02d", m) .. tag_tag[3] end if tag == nil then if d == 0 and h == 0 and m == 0 then time_str = time_str .. string.format("%02d秒", s) else time_str = time_str .. string.format("%02d", s) end else time_str = time_str .. string.format("%02d秒", s) end return time_strend--回收Lua内存function CollectMemory(t) local count = collectgarbage("count") if count >= 8192 then print("回收前:" .. count) collectgarbage("collect") print(" 回收后:" .. collectgarbage("count")) endendfunction SubStringDef(source,index,length) local r = "" if nil ~= source and index >= 0 and index <= string.len(source) then r = string.sub(source,index,length) end return rend--转换千分位加逗号function FormatNumberThousands (num) local function checknumber(value) return tonumber(value) or 0 end local formatted = tostring(checknumber(num)) local k while true do formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2') -- print(formatted,k) if k == 0 then break end end return formattedendfunction table_maxn(t) local max = nil for i, v in pairs(t) do if not max then max = v end if max < v then max = v end end return maxend-- 计算 UTF8 字符串的长度,每一个中文算一个字符function utf8len(input) local len = string.len(input) local left = len local cnt = 0 local arr = { 0, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc} while left ~= 0 do local tmp = string.byte(input, -left) local i = #arr while arr[i] do if tmp >= arr[i] then left = left - i break end i = i - 1 end cnt = cnt + 1 end return cntend
转载地址:http://rfrxo.baihongyu.com/