The four-character core comes from Tang poet
王贞白's
《白鹿洞二首》:
读书不觉已春深,
一寸光阴一寸金 - 'so absorbed in books I didn't notice spring deepen; an inch of time is an inch of gold.' The Ming-Qing household anthology
《增广贤文》 added its now-inseparable twin
寸金难买寸光阴 ('an inch of gold cannot buy an inch of time').