LoginSignup
0
0

More than 3 years have passed since last update.

各言語での日時クラスの使用感

Last updated at Posted at 2020-08-22

各言語で日時クラスの使用感に違いがあるのか?という話題があり、ざっと動かしてみたため、メモ。
自分の感覚としては日時クラスは基本、ある時刻(点)を表している認識。
('2020/8/21 23:59'で初期化した場合、 59分全体を指しているわけではなく、大体は0秒を示している認識)

Ruby

# https://docs.ruby-lang.org/ja/latest/class/Time.html

st_t = Time.new(2020,8,21,23,59)
p st_t

check_t = Time.new(2020,8,21,23,59,30)
p (st_t < check_t)
2020-08-21 23:59:00 +0900
true

JavaScript

/*
https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Date
*/
// 月は0基点
const st_t = new Date(2020, 8-1, 21, 23, 59);
console.log(st_t.toLocaleString("ja"));

const check_t = new Date(2020, 8-1, 21, 23, 59, 30);
console.log(
2020/8/21 23:59:00
true

PHP

<?php

# https://www.php.net/manual/ja/class.datetime.php

$st_t = new DateTime('2020-08-21T23:59');
echo $st_t->format('Y-m-d H:i:s.u');
echo "\n";

$check_t = new DateTime('2020-08-21T23:59:30');
echo $check_t->format('Y-m-d H:i:s.u');
echo "\n";


$res = ($st_t < $check_t);
if ($res){
  echo "true";
}
else{
  echo "false";
}
echo "\n";

2020-08-21 23:59:00.000000
2020-08-21 23:59:30.000000
true

どれも想定通り。

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