Salesforce更改Apex类中创建数据的格式
我有以下显示记录创建日期的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用这些文档作为我的参考:
“格式()”函数将完成您想要的工作。代码将是:
只需更改您使用的记录的创建日期的datetime.now()即可。
如果您真的想要这种特定格式,则必须采取一些额外的逻辑才能放入“ AT”件中。可能有更多优雅的方法可以做到这一点,但这对于我来说很容易阅读。
更新
是的,您需要使代码成为功能并将结果归还您需要的内容。这将起作用。
Using these docs as my reference:
The "format()" function will do what you want. The code would be:
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.
UPDATE
Yeah, you needed to make the code a function and return the result into what you need. This will work.