如何在MarkLogic中将字符串转换为日期类型? [英] How to convert string to date type in MarkLogic?

查看:59
本文介绍了如何在MarkLogic中将字符串转换为日期类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在将String数据类型转换为Date类型时遇到一些挑战.我使用了MarkLogic JavaScript函数xdmp.parseDateTime,但始终出现以下错误:

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:

场景:将"2013-04-21"(字符串)转换为2013-04-21(日期类型)

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

代码:

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

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

错误信息:

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

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

我是否使用了正确的MarkLogic函数,并为其提供了正确的参数? 还是有更好的方法呢?

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?

推荐答案

xs.date("2013-04-21")是xquery日期构造函数(移植到JS),采用字符串并返回xs:date. xs.dateTime("2013-04-21T00:00:00")会为您提供xs:dateTime.

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可以将字符串从更多格式转换为xs:dateTime,第二项是字符串:xdmp.parseDateTime("[Y0001]-[M01]-[D01]", targetDateString)

xdmp.parseDateTime can turn a string to xs:dateTime from more formats, the second term is a string: xdmp.parseDateTime("[Y0001]-[M01]-[D01]", targetDateString)

请参见 https://docs.marklogic.com/xdmp.parseDateTime

转换回字符串只是fn.string(yourdate)

这篇关于如何在MarkLogic中将字符串转换为日期类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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