Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- daysinmonth = {31,28,31,30,31,30,31,31,30,31,30,31};
- intmonthnames = {"January" , "February", "March" , "April",
- "May" , "June" , "July" , "August",
- "September", "October" , "November", "December"};
- weekdaynames = {[0]="Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
- currentyear = tonumber(os.date("%Y"));
- currentmonth = tonumber(os.date("%m"));
- currentday = tonumber(os.date("%d"));
- currentweekday = tonumber(os.date("%w"));
- function dateYesterday()
- local cd = currentday;
- local cm = currentmonth;
- local cwd = currentweekday;
- local cy = currentyear;
- local yd,ym,ywd,yy; -- previous day, previous month, previous weekday, previous year
- if cd > 1 then-- if current day is greater than one
- yd = cd - 1; -- previous day equals current day minus one
- if cwd > 0 then -- if current week day greater than zero
- ywd = cwd - 1; -- previous weekday equals current weekday minus one
- else
- ywd = 6; -- previous weekday equals six
- end
- else -- if current day equals one, we have to go back to the previous month
- if cm > 1 then -- if current month greater than one
- ym = cm - 1; -- previous month equals current month minus one
- if ym == 2 then -- if previous month equals 2
- if ( cy % 4 ) == 0 then -- we would have a leap year.. ignoring 400 year leaps
- cd = 29;
- else
- cd = daysinmonth[ym];
- end
- end
- else -- if current month equals one
- yy = cy - 1; -- previous year equals current year minus one
- ym = 12; -- previous month equals twelve
- yd = 31; -- december has 31 days so yesterday was the 31th
- end
- end
- return yd,ym or cm,ywd,yy or cy;
- end
- function prettyPrintDate(day,month,weekday,year)
- -- weekdaynames = {[0]="Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
- -- intmonthnames = {"January" , "February", "March" , "April",
- -- "May" , "June" , "July" , "August",
- -- "September", "October" , "November", "December"};
- assert(day and month and weekday and year);
- local s = " the ";
- local d = day;
- s = (d=="01" or d=="21" or d=="31") and s..d.."st" or
- (d=="02" or d=="22") and s..d.."nd" or
- (d=="03" or d=="23") and s..d.."rd" or
- s..d.."th";
- print(weekdaynames[weekday]..s.." of "..intmonthnames[month].." in the year "..year);
- end
- prettyPrintDate(currentday,currentmonth,currentweekday,currentyear);
- prettyPrintDate(dateYesterday());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement