首页 \ 问答 \ SQL:如何生成下个日期的日期(SQL: How to produce next date given month and day)

SQL:如何生成下个日期的日期(SQL: How to produce next date given month and day)

在我的表中,我有一个月(tinyint)和一天(tinyint)的领域。 我想有一个函数,它需要这个月份和日期,并为这个月份和下一天的下一个日期(包括年份)生成一个日期时间。

所以如果我有月份= 9,日= 7,它会产生9/7/2009。
如果我有第一个月,第一天它会产生2010年1月1日。


In my table I have a Month(tinyint) and a Day(tinyint) field. I would like to have a function that takes this month and day and produces a datetime for the next date(including year) given this month and day.

So if I had Month = 9, Day = 7 it would produce 9/7/2009.
If I had Month 1, Day 1 it would produce 1/1/2010.


原文:https://stackoverflow.com/questions/1034901
更新时间:2022-02-27 16:02

最满意答案

在javascript中,你不能使用变量作为对象文字中的属性名称,这就是你正在做的。

尝试一下:

var a = 'someProperty';

var o = {a: 'somePropertyValue'};

console.log(o);

将打印{ a: 'somePropertyValue' }不是{someProperty:'somePropertyValue}

如果javascript允许在对象文字表示法中引用属性名称中的变量,则它必须除去未加引号的名称,因为这会导致不明确的名称。 a应该用作属性的值还是应该是变量a的值?

尝试使用事先创建的对象crate创建更新对象文本,而不使用对象文字符号,因此您的代码如下所示:

router.get('/vote/:Id/:Value/:Type', function(req, res) {
    var db = req.db;
    var id = req.params.Id;
    var type = req.params.Type;
    var value = parseInt(req.params.Value);
    var newValue = value + 1;
    var collection = db.get('games');

    //We create the $set property/value pair using property assignment, not the object literal
    var updateVal = {};
    updateVal[type] = newValue;

    collection.update(
        {"_id" : id},
        {$set: updateVal}   //use it here
        , function (err, doc) {
            if (err) {
                res.send("There was a problem");
            }
            else {
                res.location("../../../admin");
                res.redirect("../../../admin");
            }
        }
    );
});

更好的是,预先构建整个$set操作。


In javascript you cannot use variables as property names in object literals, and that's what you're trying to do.

Try it:

var a = 'someProperty';

var o = {a: 'somePropertyValue'};

console.log(o);

will print { a: 'somePropertyValue' } not {someProperty:'somePropertyValue}.

If javascript permitted referencing variables in property names in object literal notation it would have to get rid of unquoted names as those would create ambiguity. Should a be used as the value of the property or should it be the value of the variable a?

Try creating the update object literal with an object crated beforehand without the usage of object literal notation, so your code looks something like this:

router.get('/vote/:Id/:Value/:Type', function(req, res) {
    var db = req.db;
    var id = req.params.Id;
    var type = req.params.Type;
    var value = parseInt(req.params.Value);
    var newValue = value + 1;
    var collection = db.get('games');

    //We create the $set property/value pair using property assignment, not the object literal
    var updateVal = {};
    updateVal[type] = newValue;

    collection.update(
        {"_id" : id},
        {$set: updateVal}   //use it here
        , function (err, doc) {
            if (err) {
                res.send("There was a problem");
            }
            else {
                res.location("../../../admin");
                res.redirect("../../../admin");
            }
        }
    );
});

Even better, construct the whole $set operation beforehand.

相关问答

更多
  • 在javascript中,你不能使用变量作为对象文字中的属性名称,这就是你正在做的。 尝试一下: var a = 'someProperty'; var o = {a: 'somePropertyValue'}; console.log(o); 将打印{ a: 'somePropertyValue' }不是{someProperty:'somePropertyValue} 。 如果javascript允许在对象文字表示法中引用属性名称中的变量,则它必须除去未加引号的名称,因为这会导致不明确的名称。 a ...
  • 您可以使用$push或$addToSet数组运算符通过单个findAnyModify调用直接将新影片添加到watched_movies数组: var movie = { "name" : "InterStellar", "Year" : "2014" }; collection.findAndModify( {'_id':new BSON.ObjectID(id)}, [['_id','asc']], {$addToSet: {"watched_movies": movie}}, ...
  • 这样的事情对你有用。 查找并替换所选房间的患者。 department.findOneAndUpdate( { "rooms.patient.patientnr": parseInt(req.params.id) }, { "$set": {"rooms.$.patient": patient}}, {new : true} .... ) Something like this should work for you. Finds and replace the patie ...
  • 我尝试了一些不同的东西,似乎猫鼬不喜欢$。 当我在做“mPlayers.1”时,它就像一个魅力:) I tried some different things and it seems like mongoose doesn't like the $. When I'm doing "mPlayers.1" it works like a charm :)
  • 在您的第一个代码示例中,您说: 为此我得到的文字没有定义 我猜你的意思是“ 测试未定义?” 您的脚本只需要mongodb库,我不相信test是核心nodejs函数,因此可以解释错误。 要引用db.collection()的驱动程序文档,请使用assert库,但也要正确导入(如第二个示例中所做的那样)。 在回调db.open() ,不检查是否发生了错误。 这可能会说明为什么db在该函数中为null。 关于与MongoDB的SQL注入等效的问题,主要关注的领域是你可以将不受信任的输入传递给已评估的JavaScr ...
  • 由于您使用的是mongo,因此可以将所有这些键放在索引中。 如果您的文档如下所示: doc = { 'users': 50, 'page': 10, 'articles': 20 } 干得好: db.foo.ensureIndex( { 'users': 1, 'page': 1, 'articles': 1 }) 然后确保只返回这些键。 如果它们在索引中,Mongo将直接从内存而不是从磁盘读取这些值。 如果要通过类似_id的方式查询,只需将_id添加为索 ...
  • 这实际上只是一个执行顺序问题。 MongoClient.connect(...)调用是异步的 。 在建立连接之前,不会调用您传递的回调函数。 但是,在调用回调之前 ,会立即调用最后的console.log(global.db) 。 实际上你不需要在这里明确地创建db global。 在模块的外部范围中定义的变量位于模块的全局范围内。 将值附加到global使其可以跨模块使用,但是在模块之间公开值的更好方法是将它们附加到exports ,以便可以在需要时显式导入它们。 如果您只在此模块中使用db ,那么这不是 ...
  • 节点不会增加4个小时。 两者都显示完全相同的瞬间。 2014-10-01 10:28:04.329-04:00 与...完全相同 2014-10-01T14:28:04.329Z 只有一个在EDT时区中,它与UTC的-04:00偏移(所以它早于4小时),另一个是UTC。 您可能在EDT中配置了服务器,并且您的客户端设置为UTC或反之亦然。 除非你需要完全相同的字符串,否则我不担心。 或者,更好的是,将客户端和服务器计算机设置为相同的时区, 最好是UTC 。 Node isn't adding 4 hou ...
  • 您正尝试在浏览器上运行服务器JavaScript代码。 JavaScript不再是客户端脚本语言。 nodeJS使用JavaScript来运行服务器框架,并且日益流行。 我现在不知道如何使用驱动程序..我试过这个: 不熟悉nodeJS这个错误。 您必须了解nodeJS与提供HTML页面的任何其他服务器一样。 在服务器上,您有服务器端脚本,执行服务器以及服务器提供的客户端内容。 只有JavaScript在nodeJS中的服务器和客户端上nodeJS 。 在学习使用mongodb软件包之前,您应该学习如何使用n ...
  • 首次创建collection时, Indexes (如在任何数据库中)只需创建一次。 索引创建是一个代价高昂的操作和阻止操作。 从mongoDB 3.0 , createIndex()和ensureIndex()方法之间没有区别,并且它们中的任何一个都应该用于在集合上创建索引(仅在服务器端创建索引一次)在需要的时候。 要索引title和ingredients字段,您需要在集合上创建index : db.collection.createIndex({"ingredients":"text","title": ...

相关文章

更多

最新问答

更多
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)
  • 是否可以嵌套hazelcast IMaps?(Is it possible to nest hazelcast IMaps? And whick side effects can I expect? Is it a good Idea anyway?)
  • UIViewAnimationOptionRepeat在两个动画之间暂停(UIViewAnimationOptionRepeat pausing in between two animations)
  • 在x-kendo-template中使用Razor查询(Using Razor query within x-kendo-template)
  • 在BeautifulSoup中替换文本而不转义(Replace text without escaping in BeautifulSoup)
  • 如何在存根或模拟不存在的方法时配置Rspec以引发错误?(How can I configure Rspec to raise error when stubbing or mocking non-existing methods?)
  • asp用javascript(asp with javascript)
  • “%()s”在sql查询中的含义是什么?(What does “%()s” means in sql query?)
  • 如何为其编辑的内容提供自定义UITableViewCell上下文?(How to give a custom UITableViewCell context of what it is editing?)
  • c ++十进制到二进制,然后使用操作,然后回到十进制(c++ Decimal to binary, then use operation, then back to decimal)
  • 以编程方式创建视频?(Create videos programmatically?)
  • 无法在BeautifulSoup中正确解析数据(Unable to parse data correctly in BeautifulSoup)
  • webform和mvc的区别 知乎
  • 如何使用wadl2java生成REST服务模板,其中POST / PUT方法具有参数?(How do you generate REST service template with wadl2java where POST/PUT methods have parameters?)
  • 我无法理解我的travis构建有什么问题(I am having trouble understanding what is wrong with my travis build)
  • iOS9 Scope Bar出现在Search Bar后面或旁边(iOS9 Scope Bar appears either behind or beside Search Bar)
  • 为什么开机慢上面还显示;Inetrnet,Explorer
  • 有关调用远程WCF服务的超时问题(Timeout Question about Invoking a Remote WCF Service)