用LUA寫一個日曆
-- serverTime 伺服器時間 秒
local time = os.time( pData )
--當前年份
local currentYear = pData.year
--當前月份
local currentMon = pData.month
--當前日期
local currentDay = pData.day
--儲存這個月的天數
local mondays = self:count_month_day( currentYear, currentMon )
--計算1號是周幾 週日是1週一是2,依次排序
local start = self:count_first_day_wday( currentDay, pData.wday )
print("show_month_num start, mondays, currentDay "..start.."-"..mondays.."-"..currentDay)
for i = 0, 41 do
--列
local r = i % 7
--行
local c = ( i - r ) / 7
-- 移除背景色
if self:getChildByTag( 3000 + i ) then
self:removeChildByTag( 3000 + i )
end
--新增非本月背景色
if (i <= start - 1) or (i > (start - 1 + mondays)) then
local pOther = cc.Sprite:createWithSpriteFrameName( "RE_other_month.png" )
pOther:setPosition( 180 + (r * 81), 429 - (c * 36.5))
self:addChild( pOther, 10, 3000 + i )
end
-- 添加當日背景色
if (i == start - 1 + currentDay) then
local pOther = cc.Sprite:createWithSpriteFrameName( "RE_celect.png" )
pOther:setPosition( 180 + (r * 81), 429 - (c * 36.5))
self:addChild( pOther, 10, 3000 + i )
end
-- 計算上月日期
local preMon = currentMon - 1
local preYear = currentYear
if preMon == 0 then
preYear = currentYear - 1
preMon = 12
end
local preMonDays = self:count_month_day( preYear, preMon )
--計算展示日期
local showDay = i - start + 1;
if showDay < 1 then
showDay = showDay + preMonDays
elseif showDay > mondays then
showDay = showDay - mondays
end
-- 新增展示label
-- 移除label
if self:getChildByTag( 5000 + i ) then
self:removeChildByTag( 5000 + i )
end
local pNum_Rili = cc.LabelTTF:create( showDay, "Arial", 24 )
pNum_Rili:setPosition( 180 + (r * 81), 429 - (c * 36.5))
self:addChild( pNum_Rili, 11, 5000 + i)
if (i <= start - 1) or (i > (start - 1 + mondays)) then
pNum_Rili:setColor( cc.c3b(255, 255, 225) )
pNum_Rili:setOpacity( 125 )
elseif (i == start - 1 + currentDay) then
pNum_Rili:setColor( cc.c3b(65, 139, 231) )
pNum_Rili:setOpacity( 255 )
end
-- 移除圈
if self:getChildByTag( 4000 + i ) then
self:removeChildByTag( 4000 + i )
end
-- 新增圈
if self.pSaveTable and table.getn(self.pSaveTable) > 0 then
if ((i - (start - 1)) > 0) and ((i - (start - 1)) <= table.getn(self.pSaveTable)) then
print("self.pSaveTable["..(i - (start - 1)).."] = "..self.pSaveTable[i - (start - 1)])
if self.pSaveTable[i - (start - 1)] == 1 then
local pSprite = cc.Sprite:createWithSpriteFrameName( "RE_yiqiandao.png" )
pSprite:setPosition( 180 + (r * 81), 429 - (c * 36.5))
self:addChild( pSprite, 11, 4000 + i )
end
end
end
end
end
function everydayRegisterLayer:getNewDate( srcDateTime, interval, dateUnit, type )
--從日期字串中截取出年月日時分秒
local Y = string.sub(srcDateTime,1,4)
local M = string.sub(srcDateTime,5,6)
local D = string.sub(srcDateTime,7,8)
local H = string.sub(srcDateTime,9,10)
local MM = string.sub(srcDateTime,11,12)
local SS = string.sub(srcDateTime,13,14)
--把日期時間字串轉換成對應的日期時間
local dt1 = os.time{ year=Y, month=M, day=D }
--根據時間單位和偏移量得到具體的偏移資料
local ofset=0
if dateUnit =='DAY' then
ofset = 60 *60 * 24 * interval
elseif dateUnit == 'HOUR' then
ofset = 60 *60 * interval
elseif dateUnit == 'MINUTE' then
ofset = 60 * interval
elseif dateUnit == 'SECOND' then
ofset = interval
end
--指定的時間+時間偏移量
--如果type == 1 是往後減幾天,其他的是往前加 幾天
local newTime = nil
if type == 1 then
newTime = os.date("*t", dt1 - tonumber(ofset))
else
newTime = os.date("*t", dt1 + tonumber(ofset))
end
return newTime
end
function everydayRegisterLayer:count_first_day_wday( currenyDay, weekDay )
print("count_first_day_wday currenyDay, weekDay "..currenyDay.."-"..weekDay)
--通過 今天 是 周幾 來計算 1號 是周幾
local p1 = ( currenyDay - 1 ) % 7
local pWeek = weekDay - p1
local pWeekNum = 0
if pWeek <= 0 then
pWeekNum = pWeek + 7
else
pWeekNum = pWeek
end
return (pWeekNum - 1)
end
function everydayRegisterLayer:count_month_day( year, mon )
if mon == 2 then
if year % 4 == 0 then
return 29
else
return 28
end
elseif mon == 1 or mon == 3 or mon == 5 or mon == 7 or mon == 8 or mon == 10 or mon == 12 then
return 31
elseif mon == 4 or mon == 6 or mon == 9 or mon == 11 then
return 30
end
end
記錄一下。