How to convert string to date type in MarkLogic?

对着背影说爱祢 提交于 2020-01-03 21:47:53

问题


I am currently having some challenge in converting String data type to Date type. I used the MarkLogic JavaScript function xdmp.parseDateTime, but I am always getting the error below:

Scenario: Convert "2013-04-21" (string) to 2013-04-21 (date type)

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

Am I using the right MarkLogic function, supplying the right parameters to it? Or is there a better way to do it?

And how do I cast a date back to a string data type?


回答1:


xs.date("2013-04-21") is the xquery date constructor (ported to JS), taking a string and returning an xs:date. xs.dateTime("2013-04-21T00:00:00") would get you an 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)

See https://docs.marklogic.com/xdmp.parseDateTime

Converting back to a string is just fn.string(yourdate)




回答2:


you can directly use the constructor of date class.

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

you can even use it with different formats, Ref.



来源:https://stackoverflow.com/questions/50149856/how-to-convert-string-to-date-type-in-marklogic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!