Salesforce更改Apex类中创建数据的格式

发布于 2025-02-12 06:29:46 字数 432 浏览 1 评论 0原文

我有以下显示记录创建日期的Apex代码:

for(let i=0; i<emailMessages.length; i++){
replyBody += 'On '+ emailMessages[i].CreatedDate + ' ' + emailMessages[i].From_Name__c +' ' +'responded: ';
}

以上代码显示这样的消息:

在2022-06-06-30T15:27:36.000Z Sarah Kat回答:

我想更改日期格式,以便显示如下:

在2022年6月30日,下午4:26萨拉·凯特(Sarah Kat)回答:

无论如何,我们可以直接在Apex中更改日期格式?

I have the following apex code that displays the created date of records:

for(let i=0; i<emailMessages.length; i++){
replyBody += 'On '+ emailMessages[i].CreatedDate + ' ' + emailMessages[i].From_Name__c +' ' +'responded: ';
}

This above code displays the message like this:

On 2022-06-30T15:27:36.000Z Sarah Kat responded:

I want to change the date format so it can be displayed as below:

On Thu, Jun 30, 2022 at 4:26 PM Sarah Kat responded:

As there anyway where we can change the date format directly in apex?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一身软味 2025-02-19 06:29:47

使用这些文档作为我的参考:

“格式()”函数将完成您想要的工作。代码将是:

Datetime createdDate = Datetime.now();
String converted = createdDate.format('E, MMM d, yyyy K:mm a');
System.debug(converted);
// Sat, Jul 2, 2022 5:21 AM

只需更改您使用的记录的创建日期的datetime.now()即可。

如果您真的想要这种特定格式,则必须采取一些额外的逻辑才能放入“ AT”件中。可能有更多优雅的方法可以做到这一点,但这对于我来说很容易阅读。

Datetime createdDate = Datetime.now();
String converted = createdDate.format('E, MMM d, yyyy K:mm a');
String[] splitup = converted.split(' ');
String final_string = '';
for(Integer i = 0; i < splitup.size(); i++)
{
    // right between year and hour
    if(i == 4)
    {
        final_string = final_string + ' at';
    }

    final_string = final_string + ' ' + splitup[i];
}
System.debug(final_string);
// Sat, Jul 2, 2022 at 5:27 AM

更新

是的,您需要使代码成为功能并将结果归还您需要的内容。这将起作用。

for(let i=0; i<emailMessages.length; i++){
    replyBody += 'On '+ datetime_formatting_email(emailMessages[i].CreatedDate) + ' ' + emailMessages[i].From_Name__c +' ' +'responded: ';
}

public String datetime_formatting_email(Datetime createdDate)
{
    String converted = createdDate.format('E, MMM d, yyyy K:mm a');
    String[] splitup = converted.split(' ');
    String final_string = '';
        for (Integer i = 0; i < splitup.size(); i++)
        {
            // right between year and hour
            if (i == 4) {
                final_string = final_string + ' at';
            }

            final_string = final_string + ' ' + splitup[i];
        }
    return final_string;
}

Using these docs as my reference:

enter image description here

The "format()" function will do what you want. The code would be:

Datetime createdDate = Datetime.now();
String converted = createdDate.format('E, MMM d, yyyy K:mm a');
System.debug(converted);
// Sat, Jul 2, 2022 5:21 AM

Just change out the Datetime.now() for the CreatedDate of the record you're using.

If you really want that specific format, you'll have to do some extra logic to put in the "at" piece. There are probably more elegant ways to do this but this was quick and easy for me to read.

Datetime createdDate = Datetime.now();
String converted = createdDate.format('E, MMM d, yyyy K:mm a');
String[] splitup = converted.split(' ');
String final_string = '';
for(Integer i = 0; i < splitup.size(); i++)
{
    // right between year and hour
    if(i == 4)
    {
        final_string = final_string + ' at';
    }

    final_string = final_string + ' ' + splitup[i];
}
System.debug(final_string);
// Sat, Jul 2, 2022 at 5:27 AM

UPDATE

Yeah, you needed to make the code a function and return the result into what you need. This will work.

for(let i=0; i<emailMessages.length; i++){
    replyBody += 'On '+ datetime_formatting_email(emailMessages[i].CreatedDate) + ' ' + emailMessages[i].From_Name__c +' ' +'responded: ';
}

public String datetime_formatting_email(Datetime createdDate)
{
    String converted = createdDate.format('E, MMM d, yyyy K:mm a');
    String[] splitup = converted.split(' ');
    String final_string = '';
        for (Integer i = 0; i < splitup.size(); i++)
        {
            // right between year and hour
            if (i == 4) {
                final_string = final_string + ' at';
            }

            final_string = final_string + ' ' + splitup[i];
        }
    return final_string;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文