2017年5月22日 星期一

[ Python 常見問題 ] How can I create basic timestamps or dates? (Python 3.4)

Source From Here 
Question 
As title. 

How-To 
Ultimately you want to review the datetime documentation and become familiar with the formatting variables, but here are some examples to get you started: 
  1. import datetime  
  2.   
  3. print('Timestamp: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now()))  
  4. print('Timestamp: {:%Y-%b-%d %H:%M:%S}'.format(datetime.datetime.now()))  
  5. print('Date now: %s' % datetime.datetime.now())  
  6. print('Date today: %s' % datetime.date.today())  
  7.   
  8. today = datetime.date.today()  
  9. print("Today's date is {:%b, %d %Y}".format(today))  
  10.   
  11. schedule = '{:%b, %d %Y}'.format(today) + ' - 6 PM to 10 PM Pacific'  
  12. schedule2 = '{:%B, %d %Y}'.format(today) + ' - 1 PM to 6 PM Central'  
  13. print('Maintenance: %s' % schedule)  
  14. print('Maintenance: %s' % schedule2)  
Output: 
Timestamp: 2017-05-22 19:36:31
Timestamp: 2017-May-22 19:36:31
Date now: 2017-05-22 19:36:31.683160
Date today: 2017-05-22
Today's date is May, 22 2017
Maintenance: May, 22 2017 - 6 PM to 10 PM Pacific
Maintenance: May, 22 2017 - 1 PM to 6 PM Central

Reference - strftime() and strptime() Behavior

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...