Convert javascript to date object to mysql date format (YYYY-MM-DD)

前端 未结 14 950
清歌不尽
清歌不尽 2020-12-13 18:04

I\'m trying to use javascript to convert a date object into a valid mysql date - what is the best way to do this?

14条回答
  •  臣服心动
    2020-12-13 18:29

    just this :

    Object.defineProperties( Date.prototype ,{
        date:{
             get:function(){return this.toISOString().split('T')[0];}
        },
        time:{
             get:function(){return this.toTimeString().match(/\d{2}:\d{2}:\d{2}/)[0];}
        },
        datetime:{
             get : function(){return this.date+" "+this.time}
        }
    });
    

    now you can use

    sql_query = "...." + (new Date).datetime + "....";
    

提交回复
热议问题