简体   繁体   English

日计算日期的名称

[英]Day Name for a calculated date

Hi Im new to Java and Im trying to workout the Day Name for a calculated date. 嗨我是Java的新手,我正在尝试为计算日期锻炼日名。 Im using Esko Automation Engine to run my Java 我使用Esko Automation Engine来运行我的Java

If I create a JavaScript script based on the actual date it works out using the following function: 如果我根据实际日期创建一个JavaScript脚本,则使用以下函数:

function f () {
    var date = new Date().toString().split(' ')[0];
    return date;
}

f ();

But when I try and replace new Date() with another calculated date it fails. 但是当我尝试用另一个计算日期替换新的Date()时,它失败了。 The calculated date displays like this: 05/02/19 10:00am and is stripped out of a xml file. 计算出的日期显示如下:05/02/19 10:00 am并从xml文件中删除。 This is named ScotlandDeliveryDate 这被命名为ScotlandDeliveryDate

function f () {
    var date = ScotlandDeliveryDate ().toString().split(' ')[0];
    return date;
}

f ();

Result: 结果:

javax.script.ScriptException: <eval>:4:25 Missing space after numeric literal
var date = 05/02/19 10:00am().toString().split(' ')[0];
^ in <eval> at line number 4 at column number 25

does anyone have any ideas please. 有没有人有任何想法。

I don't know anything about "Esko". 我对“Esko”一无所知。 But in the example that you give above, you are trying to use one SmartName ( ScotlandDeliveryDate ) inside the definition of another SmartName. 但是在上面给出的示例中,您尝试在另一个SmartName的定义中使用一个SmartName( ScotlandDeliveryDate )。 At the link below, they seem to indicate that this is done by enclosing the SmartName in brackets. 在下面的链接中,它们似乎表明这是通过将SmartName括在括号中来完成的。

https://docs.esko.com/docs/en-us/automationengine/18/userguide/home.html#en-us/common/ae/concept/co_ae_SN_Script_Exa.html https://docs.esko.com/docs/en-us/automationengine/18/userguide/home.html#en-us/common/ae/concept/co_ae_SN_Script_Exa.html

So I think your code needs to look like this: 所以我认为你的代码需要看起来像这样:

function f () {
   return '[ScotlandDeliveryDate]'.split(' ')[0];
}
f ();

However, from your error message, it looks like [ScotlandDeliveryDate] will return 05/02/19 10:00am ; 但是,从您的错误消息中,看起来[ScotlandDeliveryDate]将于05/02/19 10:00am返回; If that's the case, then splitting that value on spaces and then grabbing the first value still won't get you the day name. 如果是这种情况,那么将该值拆分为空格然后抓取第一个值仍然不会得到日期名称。

To do that, you'll need something like this: 要做到这一点,你需要这样的东西:

function f () {
   var monthDayYear = '[ScotlandDeliveryDate]'.split(' ')[0];
   var dateTimeStamp = Date.parse(monthDayYear);
   var dateObject = new Date(dateTimeStamp);
   return new Intl.DateTimeFormat('en-US', {weekday:'long'}).format(dateObject);
}
f ();

Or, without the Intl object: 或者,没有Intl对象:

function f () {
   var dayNames = [
      'Sunday',
      'Monday',
      'Tuesday',
      'Wednesday',
      'Thursday',
      'Friday',
      'Saturday'
   ];
   var monthDayYear = '[ScotlandDeliveryDate]'.split(' ')[0];
   var dateTimeStamp = Date.parse(monthDayYear);
   var dateObject = new Date(dateTimeStamp);
   var dayNumber = dateObject.getDay();
   return dayNames[dayNumber];
}
f ();

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

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