STR_TO_DATE not working with the node js

时间:2017-12-18 07:45:41

标签: javascript mysql node.js date

When I am tring to run the following query in mysql

string dateBirth = ddlDay.SelectedItem.ToString() + "/" + ddlMonth.SelectedItem.ToString() + "/" + ddlYear.SelectedItem.ToString();
DateTime dt = DateTime.ParseExact(dateBirth, "dd/MM/yyyy", null);

It is working as expected but when I am running the same using node js as

  select STR_TO_DATE(m.plan_start_date,'%Y-%m-%d') as plan_start_date from MemberDetails m ;

But then it is returning as the data present in db. Like that

         return dbConnection.query("select STR_TO_DATE(m.plan_start_date,'%Y-%m-%d') as plan_start_date from MemberDetails m ",callback);

It is sending the data in different format and sending date as string instead of date.

package.json

{
    "status": true,
    "members": [
        {
            "plan_start_date": "2017-12-27T18:30:00.000Z"
        }

    ]
}

npm version: 5.5.1 node: v8.9.1

1 个答案:

答案 0 :(得分:2)

您没有使用正确的功能,因为plan_start_date是您应该使用DATE_FORMAT的日期时间:

  SELECT  DATE_FORMAT(m.plan_start_date,'%Y-%m-%d') as plan_start_date
  FROM MemberDetails m ;

如果您想要日期而不是日期时间,可以使用功能日期:

  SELECT  DATE(m.plan_start_date) as plan_start_date
  FROM MemberDetails m ;
相关问题