Paepoi

Paepoi » JavaScript Tips » JavaScript Date

JavaScript Date

# 最終更新日 2019.03.30

Date の覚書

現在の日付と時刻
const WEEKS = ['日','月','火','水','木','金','土'];

let now = new Date();

let year  = now.getFullYear(); //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}`);

処理の時間を測る
let start = Date.now()

// 何か処理する

print(Date.now() - start);

strftime
/**
 * datetime.now('strftime_format');
 * で現在の日付と日時を得る関数のサンプル
 * f は Date の都合で三桁までしか表示できません
 * c,x は C や Python の strftime とは表示が違います
 * z,j,u,w は実装していません
 */
const datetime = {
    now(fmt) {
        let now = new Date();
        let kwd = false;
        let res = [];
        for (let s of fmt) {
            if (kwd) {
                switch (s) {
                case 'a':
                    let weeks = ['日','月','火','水','木','金','土'];
                    s = weeks[now.getDay()];
                    break;
                case 'A':
                    let lweeks = ['日曜日','月曜日','火曜日','水曜日','木曜日','金曜日','土曜日'];
                    s = lweeks[now.getDay()];
                    break;
                case 'w':
                    s = now.getDay();
                    break;
                case 'd':
                    s = now.getDate();
                    if (s < 10) s = '0' + s;
                    break;
                case 'b':
                case 'B':
                    s = (now.getMonth() + 1) + '月';
                    break;
                case 'm':
                    s = now.getMonth() + 1;
                    if (s < 10) s = '0' + s;
                    break;
                case 'y':
                    s = now.getYear() - 100;
                    break;
                case 'Y':
                    s = now.getFullYear();
                    break;
                case 'H':
                    s = now.getHours();
                    if (s < 10) s = '0' + s;
                    break;
                case 'l':
                    s = now.getHours();
                    if (s > 12) s -= 12;
                    if (s < 10) s = '0' + s;
                    break;
                case 'p':
                    if (now.getHours() < 12)
                        s = '午前';
                    else
                        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;
                case 'f':
                    s = ('00' + now.getMilliseconds()).slice(-3)
                    break;
                case 'c':
                    s = now.toLocaleString();
                    break;
                case 'x':
                    s = now.toLocaleDateString();
                    break;
                case 'X':
                    s = now.toLocaleTimeString();
                    break;
                case '%':
                    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('%c'))

Copyright(C) sasakima-nao All rights reserved 2002 --- 2020.