0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

時刻表記文字列に対し一日の区切り時刻を変更して返す

Last updated at Posted at 2020-11-15

文字列中の時刻表記部分に対して、引数で指定した時刻を日界時刻(1日の区切り時刻)とした時刻に変更した文字列を返します。
例えば引数として04:00を指定した場合は03:5927:59に、04:00はそのまま04:00として返ります。

###スクリプト###

String.prototype.boundaryTime = function(boundaryTime, f) {
    if(boundaryTime === undefined) boundaryTime = '00:00';
    if(f === undefined) f = false;
    const
        inputTime = String(this),
        boundaryArray = boundaryTime.match(/^(\d{1,2}):(\d{1,2})(:(\d{1,2}))?$/);
    if(!boundaryArray) return inputTime;

    let secondsFlag = true;
    if(boundaryArray[4] === undefined) {
        boundaryArray[4] = 0;
        secondsFlag = false;
    }

    const boundarySeconds = (boundaryArray[1] * 3600 + 
            boundaryArray[2] * 60 + Number(boundaryArray[4])) % 86400;

    return inputTime.replace(/(\d{1,2}):(\d{1,2})(:(\d{1,2}))?/g,
        function(a, h, m, sf, s) {
            if(s === undefined) s = 0;
            if(f) secondsFlag = (sf === undefined) ? false : true;
            const
                inputSeconds = h * 3600 + m * 60 + Number(s),
                resultSeconds =
                    (inputSeconds - boundarySeconds + 86400) % 86400 + boundarySeconds;

            return ('0' + Math.floor(resultSeconds / 3600) % 48).slice(-2) + ':' +
                   ('0' + Math.floor(resultSeconds / 60) % 60).slice(-2) +
                   (secondsFlag ?
                       (':' + ('0' + Math.floor(resultSeconds % 60)).slice(-2)) : '');
        });
}

Object.prototype.boundaryTime = function(boundaryTime, f) {
    if(this.toTimeString === undefined) return this;
    if(!/\d\d:\d\d:\d\d/.test(this.toTimeString())) return this;

    const result = String(this).boundaryTime(boundaryTime, f);
    const tmp = result.match(/(\d\d):\d\d(:\d\d)?/);
    if(tmp[1] > 23) {
        const d = new Date(this);
        d.setDate(d.getDate() - 1);
        return String(d).replace(/\d\d:\d\d:\d\d/, tmp[0]);
    }
    return result;
}

書式
文字列.boundaryTime(日界時刻 [,秒の有無を元の文字列側に合わせるフラグ])

console.log('00:00'.boundaryTime('02:00')); // 24:00
console.log('01:59'.boundaryTime('02:00')); // 25:59
console.log('02:00'.boundaryTime('02:00')); // 02:00
console.log('05:59'.boundaryTime('06:00')); // 29:59
console.log('06:00'.boundaryTime('06:00')); // 06:00
console.log('05:59'.boundaryTime('30:00')); // 29:59
console.log('06:00'.boundaryTime('30:00')); // 06:00

// デフォルトでは戻り値の秒の有無は引数の秒の有無で決定
console.log('06:00'.boundaryTime('06:00:00')); // 06:00:00
console.log('06:00:00'.boundaryTime('06:00')); // 06:00

// 第2引数にtrueを指定した場合は秒の有無を元の文字列側で決定
console.log('18:00~2:30:40'.boundaryTime('06:00', true)); // 18:00~26:30:40

console.log('11:59:59'.boundaryTime('12:00:00')); // 35:59:59
console.log('12:00:00'.boundaryTime('12:00:00')); // 12:00:00
console.log('23:59:59'.boundaryTime('12:00:00')); // 23:59:59
console.log('00:00:00'.boundaryTime('12:00:00')); // 24:00:00

const d = new Date();
console.log(d); // Sun Nov 15 2020 02:40:25 GMT+0900 (日本標準時)
console.log(d.boundaryTime('03:00')); // Sat Nov 14 2020 26:40 GMT+0900 (日本標準時)
console.log(d.boundaryTime('03:00:00')); // Sat Nov 14 2020 26:40:25 GMT+0900 (日本標準時)

console.log(new Date('2020-12-01 00:59:00').boundaryTime('01:00')); // Mon Nov 30 2020 24:59 GMT+0900 (日本標準時)
console.log(new Date('2020-12-01 01:00:00').boundaryTime('01:00')); // Tue Dec 01 2020 01:00 GMT+0900 (日本標準時)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?