zoukankan      html  css  js  c++  java
  • Lua常用时间函数

    原文引自:https://www.jb51.net/article/64466.htm

    时间函数

    -- 获取当前的格林尼治时间
    print(os.time())
     
    -- 获取当前时间的字符串表示,形如:11/28/08 10:28:37
    print(os.date())
     
    -- 获取当前日期的字符串表示,形如:11/28/08
    print(os.date("%x", os.time()))
     
    -- 获取当前时间的字符串表示,形如:10:28:37
    print(os.date("%X", os.time()))
     
    -- 获取当前时间的字符串表示,形如:10/10/13 10:28:37
    print(os.date("%c", os.time()))
     
    -- 获取当前时间的字符串表示,形如:2013-10-10 10:28:37
    print(os.date("%Y-%m-%d %H:%M:%S", os.time()))
     
    --函数os.clock返回执行该程序CPU花去的时钟秒数
    local x1 = os.clock()
    local s = 0
    for i = 1, 10000000 do
    s = s + i
    end
    local x2 = os.clock()
    print(string.format("elapsed time: %.2f
    ", x2 - x1))
    local T2009_StartTime = { year=2013, month=2, day=9, hour=0, min=0, sec=0 }
    local T2009_EndTime = { year=2013, month=2, day=17, hour=23, min=59, sec=59 }
     
    T2009_AvtivityTime = { startTime = os.time(T2009_StartTime), endTime = os.time(T2009_EndTime) }
     
    print('加载礼包活动成功,活动时间:' .. os.date('%c', T2009_AvtivityTime.startTime) ..
    '~' .. os.date('%c', T2009_AvtivityTime.endTime))
     
    temp = os.date("*t", os.time())
    print(temp)
    --[[则会产生表
    {year = 1998, month = 9, day = 16, yday = 259, wday = 4,
     hour = 23, min = 48, sec = 10, isdst = false}
    --]]

    字符串与时间戳转化

    时间戳转成格式化字符串

    local timestamp = 1561636137;
    local strDate = os.date("%Y/%m/%d %H:%M:%S", timestamp)
    print("strDate = ", strDate);

    字符串转化成时间戳

    分离

    local strDate = "2019/06/27 19:48:57"
    local _, _, y, m, d, hour, min, sec = string.find(strDate, "(%d+)/(%d+)/(%d+)%s*(%d+):(%d+):(%d+)");   --分离字符串 前两个不知道是什么?
     
    print(y, m, d, hour, min, sec);

     转化

    --转化为时间戳
    local timestamp = os.time({year=y, month = m, day = d, hour = hour, min = min, sec = sec});
    print("timestamp = ", timestamp);
  • 相关阅读:
    ASP.NET 取得 Request URL 的各个部分
    将GAC中的DLL复制出来
    SqlServer2008 手动提交
    SQL获取表中最新插入的记录
    HTTP/1.1 500 Server Error错误解决方法
    ORACLE 物化视图
    JIMMY ZHANG告诉你快速提高自己的开发能力
    走进设计模式系列之开篇
    大话设计模式之:Adapter模式
    jquery+bootstrap自定义插件开发之dropdownlist
  • 原文地址:https://www.cnblogs.com/yifengs/p/14922538.html
Copyright © 2011-2022 走看看