LoginSignup
0

More than 5 years have passed since last update.

jsで当日0時0分からの経過分を得る

Last updated at Posted at 2018-06-08

今日の0時からの経過分をjavascriptで得る

jsのDateクラスはUNKなので力技で解決する(タイムゾーン周りはブラウザ依存で結構危ういので要検証)。
他言語でもdiffしてformatして泥臭くなりがちなのでしょうがない。

解説

var time1 = new Date();
console.log(time1);
var time2 = time1.getTime();//unixtimeを得る
var time3 = time2 % (1000 * 60 * 60 * 24);//時分秒msを得る
var time4 = Math.floor(time3 / (1000 * 60));//秒msを切捨て
var time5 = time4 - time1.getTimezoneOffset();//タイムゾーン分を加算(減算)
console.log(time5);

//Fri Jun 08 2018 13:20:01 GMT+0900 (東京 (標準時))
//800

hint
経過秒ならtime41000だけにすると得られる。
経過時ならtime41000 * 60 * 60すると得られる。
※そのときはタイムゾーンは分なので位合わせを忘れずに

コンパクト

var time = new Date();
console.log(
    Math.floor((time.getTime() % (1000 * 60 * 60 * 24)) / (1000 * 60)) - time.getTimezoneOffset()
);

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