Python 升华文本中的时间增量不工作

Python 升华文本中的时间增量不工作,python,datetime,sublimetext2,sublimetext,timedelta,Python,Datetime,Sublimetext2,Sublimetext,Timedelta,我已将上述代码保存为time.py 并将命令映射到热键,以便在编辑器中插入昨天和明天的日期 我在这里做错了什么?您需要添加括号以将日期时间算术组合在一起: import datetime, getpass from datetime import timedelta import sublime, sublime_plugin class TommorowCommand(sublime_plugin.TextCommand): def run(self, edit):

我已将上述代码保存为
time.py
并将命令映射到热键,以便在编辑器中插入昨天和明天的日期


我在这里做错了什么?

您需要添加括号以将
日期时间
算术组合在一起:

import datetime, getpass from datetime 
import timedelta import sublime, sublime_plugin

class TommorowCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        self.view.run_command("insert_snippet", { "contents": "%s" %  datetime.date.today() + datetime.timedelta(days=1) } )

class YesterdayCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        self.view.run_command("insert_snippet", { "contents": "%s" %  datetime.date.today() - datetime.timedelta(days=1) } )
因为
+
运算符的优先级低于
%
运算符:

{"contents": "%s" % (datetime.date.today() + datetime.timedelta(days=1))}

谢谢,这解决了我的问题。{“contents”:str(datetime.date.today()+datetime.timedelta(days=1))}{“contents”:str(datetime.date.today()-datetime.timedelta(days=1))}datetime.datetime.now().strftime(“%A,%d%b%Y”)给了我这个星期四,2013年11月14日,有没有办法用这种格式str(datetime.datetime.date.today().strftime().strftime(“%A,%d%b%Y”)获取明天和昨天+datetime.timedelta(days=1))似乎没有给出我想要的结果,2013年11月15日星期五。因此,根据减法的结果调用它(因此在这里也使用括号):
(datetime.date.today()+datetime.timedelta(days=1)).strftime(“…”)
非常感谢您的时间,我得到了所有答案:)我刚刚开始全面探索sublime文本,它使用python作为插件,我是一个DotNet的家伙,几乎没有做任何python脚本。
>>> "%s" % datetime.date.today() + datetime.timedelta(days=1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'datetime.timedelta' objects
>>> "%s" % (datetime.date.today() + datetime.timedelta(days=1))
'2013-11-14'
{"contents": str(datetime.date.today() + datetime.timedelta(days=1))}