简体   繁体   English

从字符串javascript转换为访问datetime

[英]convert from string javascript to access datetime

I am working with ADODB to connect my javascript with ms.access (hell yeah I know it shouldn't use js) and using in IE8. 我正在使用ADODB将我的JavaScript与ms.access连接(是的,我知道它不应该使用js)并在IE8中使用。 I can connect, insert, update, delete well. 我可以很好地连接,插入,更新,删除。 But the problem when I want to insert or update specific row with datatype datetime in access, can you help me how to convert datatype string so it can compatible in datetime ms.access. 但是,当我想在访问中插入或更新数据类型为datetime的特定行时,该问题可以帮助我如何转换数据类型字符串,以便它在datetime ms.access中兼容。

I have tried to convert it into Date, but the error still show type mismatch... and here is my code 我试图将其转换为日期,但错误仍然显示类型不匹配...这是我的代码

 var adoConn = new ActiveXObject("ADODB.Connection"); var adoRS = new ActiveXObject("ADODB.Recordset"); adoConn.Open(conString); adoRS.Open("SELECT * FROM tableName", adoConn, 1, 3); var s = new Date("11/27/2014"); adoRS.AddNew; adoRS.Fields("myDateColumn").value = s; adoRS.Update; adoRS.Close(); adoRS = null; adoConn.Close(); adoConn = null; 
I already have searched through some website that explains connection to access, eventhough they don't give some examples about datetime problem, thanks before 我已经搜索了一些解释访问权限的网站,尽管他们没有提供有关日期时间问题的示例,谢谢

试试这个代码:

new Date().toISOString().replace('T', ' ').slice(0, -5)

JScript Date对象提供了getVarDate方法,用于将Date转换为VT_DATE格式(ADODB期望):

adoRS.Fields("myDateColumn").value = s.getVarDate();

Ahh finally I know my bad. 终于,我知道我的坏事。 I should try not to convert into date first, I try to insert with datatype string directly and it really shock me, because success! 我不应该尝试先不转换为日期,我尝试直接使用数据类型字符串插入,这确实令我震惊,因为成功! Furthermore if you want to insert date with value "1/2/2014", try to change it into 2 digit in month and date value. 此外,如果要插入值“ 1/2/2014”的日期,请尝试将其更改为2位数的月份和日期值。 So it must be "01/02/2014" 因此它必须是“ 01/02/2014”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM