Javascript date to sql date object

前端 未结 3 1666
面向向阳花
面向向阳花 2020-12-09 17:07

I\'m trying to write a query that takes a Javascript date object and then puts it in an object type that is recognized by both SQL Server and Oracle database types.

相关标签:
3条回答
  • 2020-12-09 17:26

    May be I found a bit shorter approach - tested on MSSQL and Postgres

    const date = (new Date()).toLocaleString("en-US")
    

    Enjoy!

    0 讨论(0)
  • 2020-12-09 17:27

    using MomentJs it will be pretty easy.

    moment().format('YYYY-MM-DD HH:MM:SS')
    

    https://momentjs.com/

    0 讨论(0)
  • 2020-12-09 17:36

    Have you tried the solutions presented here:

    Convert JS date time to MySQL datetime

    The title should be called

    "Convert JS date to SQL DateTime"

    I happened to need to do the same thing as you just now and I ran across this after your question.

    This is from the other post by Gajus Kuizinas for those who want the answers on this page:

    var pad = function(num) { return ('00'+num).slice(-2) };
    var date;
    date = new Date();
    date = date.getUTCFullYear()        + '-' +
            pad(date.getUTCMonth() + 1) + '-' +
            pad(date.getUTCDate()       + ' ' +
            pad(date.getUTCHours()      + ':' +
            pad(date.getUTCMinutes()    + ':' +
            pad(date.getUTCSeconds();     
    

    or

    new Date().toISOString().slice(0, 19).replace('T', ' ');
    

    The first one worked for me. I had a reference problem with the toISOString as well although I would prefer the one liner. Can anyone clarify how to use it and know the limitations on where one can reference it? Good luck!

    0 讨论(0)
提交回复
热议问题