Ben Chuanlong Du's Blog

And let it direct your passion with reason.

Generating YYYYMM Formatted Dates Using Python

import monthdelta as md
import datetime as dt
import math as math

def quarter(month):
    return int(math.ceil(month/3.0))
#end def

d0 = md.date.today()
dates = [d0 + md.monthdelta(i) for i in range(1, 20)]
yyyymms = [d.year*100 + d.month for d in dates]
yymms = [d.year%100 * 100 + d.month for d in dates]
print(", ".join(map(str, yyyymms)))
print(", ".join(map(str, yymms)))

The between ... and clause is also convenient to work with YYYYMM numbers I saw people use

where m in &months.;

to check .. while a much more convenient way is to use

where m between 200201 and 201212;

if you are sure m contains only YYYYMM formated numbers.

Comments