ฉันจะแสดงเวลาปัจจุบันเป็น:
12:18PM EST on Oct 18, 2010
ใน Python ขอบคุณ.
ฉันจะแสดงเวลาปัจจุบันเป็น:
12:18PM EST on Oct 18, 2010
ใน Python ขอบคุณ.
คำตอบ:
ขั้นแรกเป็นวิธีที่รวดเร็วและสกปรกและอย่างที่สองเป็นวิธีที่แม่นยำ (ตระหนักถึงการประหยัดเวลากลางวันหรือไม่)
import time
time.ctime() # 'Mon Oct 18 13:35:29 2010'
time.strftime('%l:%M%p %Z on %b %d, %Y') # ' 1:36PM EDT on Oct 18, 2010'
time.strftime('%l:%M%p %z on %b %d, %Y') # ' 1:36PM EST on Oct 18, 2010'
%z
สร้างการชดเชยเช่น-0400
. %Z
ให้ชื่อเขตเวลาปัจจุบัน (หรือตัวย่อ) โดยอัตโนมัติ Python ไม่รองรับ%l
คุณสามารถใช้%I
แทนได้
คุณสามารถทำสิ่งต่างๆเช่น:
>>> from time import gmtime, strftime
>>> strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
'Thu, 28 Jun 2001 14:17:15 +0000'
เอกสารฉบับเต็มเกี่ยวกับรหัส% อยู่ที่http://docs.python.org/library/time.html
import time
time.strftime('%H:%M%p %Z on %b %d, %Y')
เมื่อใช้รหัสนี้คุณจะได้รับเขตเวลาสดของคุณ
import datetime
now = datetime.datetime.now()
print ("Current date and time : ")
print (now.strftime("%Y-%m-%d %H:%M:%S"))