SQLite での日付、時刻の取り扱い

SQLite での日付、時刻の取り扱い
#13 日付・時刻を扱ってみよう
http://dotinstall.com/lessons/basic_sqlite/6413
を参考に
日付、時刻について学習
SQLite では、いくつかの定数で時刻を得ることができる
select current_time;
で現在時刻の取得
select current_date;
で現在の日付
select current_timestamp;
で現在の日付と時刻が表示できる
試しに
create table date(name,crated);
でテーブルを作成して
insert into date(name,crated) values(‘higuchi’,current_timestamp);
でデータ挿入
select * from date;
higuchi|2013-06-03 10:52:36
というように、現在時刻が入っていることが確認できる
日付関連リファレンスは
http://www.sqlite.org/lang_datefunc.html
を参考に
この中で一番使われるのが
strftime(format, timestring, modifier, modifier, …)
というタイプ
指定できるフォーマットは
%d
day of month: 00
%f
fractional seconds: SS.SSS
%H
hour: 00-24
%j
day of year: 001-366
%J
Julian day number
%m
month: 01-12
%M
minute: 00-59
%s
seconds since 1970-01-01
%S
seconds: 00-59
%w
day of week 0-6 with Sunday==0
%W
week of year: 00-53
%Y
year: 0000-9999
%% %
これらのフォーマットを利用して
current_timestamp を日本語表記にするには
strftime(‘%Y年’,current_timestamp)
というようにする
select current_timestamp;
の場合
2013-06-03 11:01:44
となるけど
select strftime(‘%Y年’,current_timestamp);
とすると
2013年
となる
時刻の取得関連はゲーム関連で使うことが多いので
覚えておくと便利

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です