experiment: display a monthly calendar

1

Please login or register to vote for this query.

(click on this box to dismiss)

experiment: draw current month's calendar using box drawing characters

Latin Language

Q&A for linguists, teachers, and students wanting to discuss the finer points of the Latin language

declare @columnWidth tinyint = 4
declare @startDate date, @stopDate date

if ##yearsToShow:int?5## < 0 
  begin --negative value entered
    set @startDate = dateadd(year,##yearsToShow:int?5##,getdate())
    set @stopDate = getdate()
  end
else
  begin  --positive value entered
    set @startDate = getdate()
    set @stopDate = dateadd(year,##yearsToShow:int?5##,getdate())
  end
  
declare @dt date, @firstWkDay smallint, @lastWkDay smallint
declare @WkDay smallint, @WkNum smallint, @numDays smallint, @numWks smallint
declare @st nvarchar(max), @st1 nvarchar(max), @st2 nvarchar(max), @monthLabel nvarchar(max)
declare @charsToTrim_top int, @charsToTrim_btm int, @topBorder nvarchar(max)

drawMonth:
set @startdate   = format(@startdate,'yyyy-MM-01')
set @stopdate     = eomonth(@startdate)
set @numDays     = day(@stopdate)
set @firstWkDay  = datepart(weekday,@startdate)
set @lastWkDay   = datepart(weekday,@stopdate)
set @numWks    = cast((@numDays-@lastWkDay-(8-@firstWkDay))/7 as smallint)+2
set @WkNum       = 1
set @WkDay       = 1
set @dt          = dateadd(d,1-@firstWkDay,@startdate)
set @monthLabel  = format(@startdate,'MMMM yyyy')                              --reset @st: month name/year
set @charsToTrim_top = ((@firstWkDay-1)*5)+1
set @charsToTrim_btm = ((7-@lastWkDay)*5)+1
set @topBorder   = replicate(N' ',@charsToTrim_top-1)+N'╔'+replicate(N'════╦',7-@firstWkDay)+N'════╗'+char(10)  --top borders
set @st = @topBorder

declare @Line nvarchar(30) = replicate(N'═',@columnWidth)
declare @Blank nvarchar(30) = replicate(N' ',@columnWidth)

while @wkNum <= @numWks  --if there are weeks remaining in this month...
  begin                    --...then start a new day
    if @wkDay=1 --start of week
      begin
        set @st1=N'║' 
        set @st2 = case when @wkNum<@numWks then N'╠' else N'╚' end
      end
  
    set @st1 = @st1 + case when month(@dt)=month(@startdate) then format(@dt,N' dd ') else N'    ' end
    set @st2 = @st2 + N'════' 
    
    if @wkday=7 --end of week
      begin
      
--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-- tidy unused cells
        if @wknum = 1  --first week
          begin
            set @st2 = replicate(N' ',@charsToTrim_top)+substring(@st2,@charsToTrim_top+1,999)
            set @st1=replace(@st1,N'║    ',N'     ')
            set @st2=replace(@st2,N' ═',N'╬═')
            set @st2=replace(@st2,N'     ',N'╦════')
            set @st2= case when @firstWkDay=1 then N'╠' else N'╔' end + substring(@st2,2,999)
          end
      
        if @wknum = @numWks-1  --second-to-last week
          begin
            set @st2 = left(@st2,len(@st2)-(@charsToTrim_btm-1))
            if @lastwkday < 7 
                   set @st2=@st2+N'╬'+replicate(N'════╩',7-@lastwkday-1)+N'════╝'
              else set @st2=@st2+N'╣'
          end
      
        if @wknum = @numWks  --last week
          begin
            set @st1=replace(@st1,N'    ║',N'     ')
            set @st2=left(@st2,len(@st2)- (@charsToTrim_btm-1))
          end
     
        set @st=@st+@st1+case when @wkday=7 and @lastwkday<7 and @wknum=@numwks then N'' else N'║' end+char(10)   
        set @st=@st+@st2+case when @wknum=@numwks-1 then N'' when @wknum=@numwks then N'╝' else N'╣' end+char(10)
        set @wknum=@wknum+1 --increment week#
      end
    else
      begin
        set @st1=@st1+N'║'
        set @st2=@st2+case when @wkNum<@numWks then N'╬' else N'╩' end
      end

--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-- finished tidy; increment counters     
    set @dt = dateadd(day,1,@dt)       --finished this day, increase date
    set @wkday = datepart(weekday,@dt) --increment date
  end 
--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-- print month
if left(@st,len(@monthLabel))=replicate(' ',len(@monthLabel)) --optional: overlay month into 1st week if room
       set @st = @monthLabel + substring(@st, len(@monthLabel)+1,9999)  
  else set @st = @monthLabel + char(10) + @st
--or to center:  --set @monthLabel  = replicate(' ',cast(((7*5)+1-len(@monthLabel))/2 as int))+@monthLabel+char(10) --center month name


print @st
set @startdate = dateadd(month,1,@startdate)
if @startdate < dateadd(year,##yearsToShow:int?5##,getdate()) goto drawMonth



--yearsToShow:Enter the number of years to display
/*╔═╦╗ ╔═╦╗ ╗ ╔╗ 
  ║ ║║ ║ ╚╝ ║ ║║ 
  ╠═╬╣ ╚═╦╗ ╠═╬╣ 
  ║ ╚╝ ╚═╩╝ ╝ ╚╝ 
 ═╝              */

Enter Parameters

Options:
Switch to meta site
loading Hold tight while we fetch your results