简体   繁体   English

如何在MarkLogic中将字符串转换为日期类型?

[英]How to convert string to date type in MarkLogic?

I am currently having some challenge in converting String data type to Date type. 我目前在将String数据类型转换为Date类型时遇到了一些挑战。 I used the MarkLogic JavaScript function xdmp.parseDateTime , but I am always getting the error below: 我使用了MarkLogic JavaScript函数xdmp.parseDateTime ,但我总是得到以下错误:

Scenario: Convert "2013-04-21" (string) to 2013-04-21 (date type) 场景:将“2013-04-21”(字符串)转换为2013-04-21(日期类型)

Code: 码:

let targetDateString = "2013-04-21";
let targetDate = new Date();

targetDate = xdmp.parseDateTime("[Y0001]-[M01]-[D01]", 
xs.date(targetDate));

Error Info: 错误信息:

XDMP-ARGTYPE: xdmp.parseDateTime("[Y0001]-[M01]-[D01]", xs.date("2013-04-21")) -- arg2 is not of type String XDMP-ARGTYPE:xdmp.parseDateTime(“[Y0001] - [M01] - [D01]”,xs.date(“2013-04-21”)) - arg2不是String类型

Am I using the right MarkLogic function, supplying the right parameters to it? 我使用正确的MarkLogic功能,为它提供正确的参数吗? Or is there a better way to do it? 或者有更好的方法吗?

And how do I cast a date back to a string data type? 如何将日期转换回字符串数据类型?

xs.date("2013-04-21") is the xquery date constructor (ported to JS), taking a string and returning an xs:date. xs.date("2013-04-21")是xquery日期构造函数(移植到JS),接受一个字符串并返回一个xs:date。 xs.dateTime("2013-04-21T00:00:00") would get you an xs:dateTime. xs.dateTime("2013-04-21T00:00:00")会给你一个xs:dateTime。

xdmp.parseDateTime can turn a string to xs:dateTime from more formats, the second term is a string: xdmp.parseDateTime("[Y0001]-[M01]-[D01]", targetDateString) xdmp.parseDateTime可以从更多格式将字符串转换为xs:dateTime,第二项是字符串: xdmp.parseDateTime("[Y0001]-[M01]-[D01]", targetDateString)

See https://docs.marklogic.com/xdmp.parseDateTime 请参阅https://docs.marklogic.com/xdmp.parseDateTime

Converting back to a string is just fn.string(yourdate) 转换回字符串只是fn.string(yourdate)

you can directly use the constructor of date class. 你可以直接使用date类的构造函数。

var d = new Date("2013-04-21");
console.log(d);

you can even use it with different formats, Ref . 您甚至可以使用不同的格式, 参考

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

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