Javascript日期到sql日期对象 [英] Javascript date to sql date object

查看:92
本文介绍了Javascript日期到sql日期对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个带有Javascript日期对象的查询,然后将其放入SQL Server和Oracle数据库类型都可识别的对象类型中。

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.

问题是我正在使用webservices。所以它必须是一个字符串,而不是一个实际的传递参数。这就是我的意思:

The issue is that I'm using webservices. So it has to be a string, not an actual passed parameter. Here's what I mean:

var date = new Date();
var firstDayOfMonth = new Date(date.getFullYear(), date.getMonth(), 1);
var lastDayOfMonth = new Date(date.getFullYear(), date.getMonth() + 1, 0);

var webServicesQueryWhereClause = 'readDate BETWEEN '+firstDayOfMonth+' AND '+lastDayOfMonth;

firstDayOfMonth lastDayOfMonth to_date()包围,以实际将它们置于数据库可读取的日期格式中。例如:

Except firstDayOfMonth and lastDayOfMonth are surrounded by something like to_date() to actually put them in a date format that the databases can read. For example:

var webServicesQueryWhereClause = 'readDate BETWEEN to_date('+firstDayOfMonth+') AND to_date('+lastDayOfMonth+') ';

我应该使用什么来将这些日期放在SQL Server和Oracle都能读取的表单中?

What should I use to put those dates in a form that can be read by both SQL Server and Oracle?

推荐答案

您是否尝试过此处提供的解决方案:

Have you tried the solutions presented here:

< a href =https://stackoverflow.com/questions/5129624/convert-js-date-time-to-mysql-datetime>将JS日期时间转换为MySQL日期时间

标题应该被称为

将JS日期转换为SQL DateTime

"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.

这是来自Gajus Kuizinas的另一篇文章给那些想要的人本页面的答案:

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', ' ');

第一个对我有用。我对toISOString有一个参考问题,虽然我更喜欢一个衬垫。任何人都可以澄清如何使用它并了解其局限性在哪里可以参考它?
祝你好运!

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!

这篇关于Javascript日期到sql日期对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆