Python: 現在の日付・時刻の取得と出力 – datetimeクラスの属性、today()、strftime()メソッド

Sponsored Link

このエントリーをはてなブックマークに追加
はてなブックマーク - Python: 現在の日付・時刻の取得と出力 – datetimeクラスの属性、today()、strftime()メソッド
Share on Facebook
Bookmark this on Delicious
Share on LinkedIn
Bookmark this on Livedoor Clip
Bookmark this on Yahoo Bookmark

ソースコード

#!/usr/bin/python
# coding: UTF-8

# 現在の日付・時刻の取得と出力 | datetimeクラスの属性、today()、strftime()メソッドの使い方

import datetime # datetimeモジュールのインポート
import locale   # import文はどこに書いてもOK(可読性などの為、慣例でコードの始めの方)

# today()メソッドで現在日付・時刻のdatetime型データの変数を取得
d = datetime.datetime.today()
#   ↑モジュール名.クラス名.メソッド名

print 'd == %s : %s\n' % (d, type(d)) # Microsecond(10^-6sec)まで取得

# datetime型の各属性へのアクセス
# year, month, day
print '%s年%s月%s日\n' % (d.year, d.month, d.day)

# hour, minute, second, microsecond
print '%s時%s分%s.%s秒n' % (d.hour, d.minute, d.second, d.microsecond)

# strftime()メソッドで日付時刻の書式を指定して出力
print d.strftime("%Y-%m-%d %H:%M:%S"), '\n'

# 地域の設定
locale.setlocale(locale.LC_ALL, 'ja') # 属性の出力に影響(曜日とか)
print locale.getlocale(), '\n'

print d.strftime("%B%d日%A") # locale.setlocale()でlocale指定してないと"Fri"と表示(デフォルトの設定地域)
print d.strftime("%p")       # 指定の地域でのAM/PMの対応する文字列
print d.strftime("%x %X")    #               日付と時刻に対応する文字列

実行結果

d == 2008-06-06 11:50:25.964000 : <type 'datetime.datetime'>

2008年6月6日

11時50分25.964000秒

2008-06-06 11:50:25

('Japanese_Japan', '932')

6月06日金曜日
午前
2008/06/06 11:50:25

リファレンス

チュートリアル

関連すると思われる記事:

Sponsored Link

This entry was posted in Python and tagged , , . Bookmark the permalink.

Facebook comments:

One Response to Python: 現在の日付・時刻の取得と出力 – datetimeクラスの属性、today()、strftime()メソッド

  1. Pingback: 松尾 亮

コメントを残す

メールアドレスが公開されることはありません。

次のHTML タグと属性が使えます: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>