JavaScript 覚書で jjs を削除し他を今風に書き換え。
だけじゃつまらないから何か追記したい。
日付けと時刻の取得はまだ Gjs でしかやっていなかった。
よしこれを JXA, Node.js で行う手段を探し、、、、、いやまて。
よく考えたら JavaScript には Date オブジェクトがあった。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | /** * Gjs, JXA, Node.js 共通 */ const WEEKS = [ '日' , '月' , '火' , '水' , '木' , '金' , '土' ]; let now = new Date(); let year = now.getYear() + 1900; let month = now.getMonth() + 1; let day = now.getDate(); let week = WEEKS[now.getDay()]; let hour = now.getHours(); let minut = now.getMinutes(); let sec = now.getSeconds(); if (month < 10) month = '0' + month; if (day < 10) day = '0' + day; if (hour < 10) hour = '0' + hour; if (minut < 10) minut = '0' + minut; if (sec < 10) sec = '0' + sec; if ( typeof print === 'undefined' ) print = console.log ; print ( `${year}年${month}月${day}日(${week}) ${hour}:${minut}:${sec}` ); |
で。
ウエブブラウザでも使える、JavScriptCore 単体でも動く。
.bashrc にエイリアスを作って試してみよう。
# Fedora alias alias jsc=/usr/libexec/webkit2gtk-4.0/jsc # macOS alias alias jsc=/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc
あ、仕事は遅番出勤ですので。
ところで、以下で検索するとやはりというかなんというか。
‘node.js 日付と時刻’
strftime 形式で日時取得ごときにインストール…
何故いつもいつもインストールばかりやっているのだ?
その程度を自分で作れない輩にいったい何が作れるのだ?
%H,%M,%S だけの実装例を書いてみる。
datetime.now なのはもちろん Python に合わせた。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | const datetime = { now(fmt) { let now = new Date(); let kwd = false ; let res = []; for ( let s of fmt) { if (kwd) { switch (s) { case 'H' : s = now.getHours(); if (s < 10) s = '0' + s; break ; case 'M' : s = now.getMinutes(); if (s < 10) s = '0' + s; break ; case 'S' : s = now.getSeconds(); if (s < 10) s = '0' + s; break ; default : throw 'undefined format' ; } kwd = false ; } else { if (s === '%' ) { kwd = true ; continue ; } } res.push(s); } return res.join( '' ); } } if ( typeof print === "undefined" ) print = console.log ; print (datetime.now( '%H:%M:%S' )) |
15 分で作れた。
Google さん、もっとまともなサイトを上位に表示してよ。