我有一个方法,给定一个包含内部文本中的ISO 8601日期的.NET XmlNode
,它将把它转换为X++ date
对象。
if (CLRInterop::isInitialized(childNode))
{
return str2Date(childNode.innerText(), 321);
}
else return maxDate();
如果提供了一个只包含日期(例如:2019-03-21
)的字符串,这会很好,但是只要在这个字符串中提供了一个时间(例如:2019-03-21T00:00:00
),它就不会返回任何内容。
解决这个问题的最简单的方法就是去掉前10个字符之后的所有内容,但如果由于某种原因,该字符串在一年中只包含2个字符,则会再次中断。在调用str2date
时是否有更健壮的处理字符串(包括时间)的方法
发布于 2019-03-21 00:02:13
我只是用一堆例子写了这份工作。第一行可能是你想要的。您只需将其创建为AX中的新作业,然后在第一行上放置一个断点,并逐个遍历以查看发生了什么,或修改以进行实验。
看起来您的字符串是标准ISO格式,我在下面也介绍了这些格式。
static void DateTimeJob(Args _args)
{
// This line looks about what you want
utcDateTime utcDateTimeFromString = DateTimeUtil::anyToDateTime("2019-03-21T00:00:00");
// ISO standard format. You can just assign it directly without quotes
utcDateTime utcDateTimeISOFormat = 2019-03-21T00:00:00;
// Misc vars for below
utcDateTime utcNow;
System.DateTime systemDateTime;
date dateOnly;
str systemDateTimeStr;
// Look at
// DateTimeUtil::<> // This has all sorts of useful functions
// str2datetime() // May be useful to you
try
{
// How to go from AX UTC to System.DateTime
systemDateTime = Global::utcDateTime2SystemDateTime(DateTimeUtil::utcNow());
// How to go from System.DateTime to AX UTC
utcNow = Global::clrSystemDateTime2UtcDateTime(System.DateTime::get_UtcNow());
// How to get ONLY the date portion from a UTC
dateOnly = DateTimeUtil::date(utcNow);
// Cast to string for output
systemDateTimeStr = systemDateTime.ToString();
// Output a few examples
info(strFmt("%1, %2, %3",
systemDateTimeStr,
utcNow,
dateOnly));
}
catch (Exception::CLRError)
{
error(AifUtil::getClrErrorMessage());
}
}
https://stackoverflow.com/questions/55271468
复制相似问题