Advertisement
xth

yesterday

xth
Jul 30th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.54 KB | None | 0 0
  1. daysinmonth = {31,28,31,30,31,30,31,31,30,31,30,31};
  2. intmonthnames = {"January"  , "February", "March"   , "April",
  3.                  "May"      , "June"    , "July"    , "August",
  4.                  "September", "October" , "November", "December"};
  5. weekdaynames = {[0]="Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
  6. currentyear = tonumber(os.date("%Y"));
  7. currentmonth = tonumber(os.date("%m"));
  8. currentday = tonumber(os.date("%d"));
  9. currentweekday = tonumber(os.date("%w"));
  10. function dateYesterday()
  11.   local cd = currentday;
  12.   local cm = currentmonth;
  13.   local cwd = currentweekday;
  14.   local cy = currentyear;
  15.   local yd,ym,ywd,yy;  -- previous day, previous month, previous weekday, previous year
  16.   if cd > 1 then-- if current day is greater than one
  17.     yd = cd - 1; -- previous day equals current day minus one
  18.     if cwd > 0 then -- if current week day greater than zero
  19.       ywd = cwd - 1; -- previous weekday equals current weekday minus one
  20.     else
  21.       ywd = 6; -- previous weekday equals six
  22.     end
  23.   else -- if current day equals one, we have to go back to the previous month
  24.     if cm > 1 then -- if current month greater than one
  25.       ym = cm - 1; -- previous month equals current month minus one
  26.       if ym == 2 then -- if previous month equals 2
  27.         if ( cy % 4 ) == 0 then -- we would have a leap year.. ignoring 400 year leaps
  28.           cd = 29;
  29.         else
  30.           cd = daysinmonth[ym];
  31.         end
  32.       end
  33.     else  -- if current month equals one
  34.       yy = cy - 1; -- previous year equals current year minus one
  35.       ym = 12; -- previous month equals twelve
  36.       yd = 31; -- december has 31 days so yesterday was the 31th
  37.     end
  38.   end
  39.   return yd,ym or cm,ywd,yy or cy;
  40. end
  41.  
  42. function prettyPrintDate(day,month,weekday,year)
  43.   -- weekdaynames = {[0]="Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
  44.   -- intmonthnames = {"January"  , "February", "March"   , "April",
  45.   --                  "May"      , "June"    , "July"    , "August",
  46.   --                  "September", "October" , "November", "December"};
  47.   assert(day and month and weekday and year);
  48.   local s = " the ";
  49.   local d = day;
  50.   s = (d=="01" or d=="21" or d=="31") and s..d.."st" or
  51.       (d=="02" or d=="22") and s..d.."nd" or
  52.       (d=="03" or d=="23") and s..d.."rd" or
  53.       s..d.."th";
  54.   print(weekdaynames[weekday]..s.." of "..intmonthnames[month].." in the year "..year);
  55. end
  56.  
  57. prettyPrintDate(currentday,currentmonth,currentweekday,currentyear);
  58.  
  59. prettyPrintDate(dateYesterday());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement