Moment.JS 的时间格式问题

Time formatting issues with Moment.JS

我有一些数据库对象从我的服务器返回:

client_id: 24
created_at: "2022-02-11 17:41:39.330443"
id: 22
report: "sfsf"
report_category: "Client Assigned"
volunteer_id: 23


client_id: 24
created_at: "2022-02-11 17:43:04.837869"
id: 23
report: "one more"
report_category: "Client Assigned"
volunteer_id: 23

在我的 React 代码中,我试图将带有 moment 的格式应用于 created_at 字段:

{moment(comment.createdAt).format('DD/MM/YY, h:mm a')}

其中 returns 类似于:

11/02/22, 12:46 pm

对于所有对象,时间似乎默认为当前时间,而不是 created_at 字段中的时间。所有条目显示 12:46 下午。关于这个问题有什么建议吗?

我在想可能格式有误,如果是这样的话,它只是默认为当前时间。 'a' 在格式的末尾做什么。也许尝试这样的东西“HH:mm:ss”

您的对象调用看起来不正确,可能默认为当前时间,因为在您的示例对象中:

{
  client_id: 24
  created_at: "2022-02-11 17:41:39.330443"
  id: 22
  report: "sfsf"
  report_category: "Client Assigned"
  volunteer_id: 23
}

当您可能需要 created_at:

时,您的代码会调用 createdAt
moment(comment.created_at).format('DD/MM/YY, h:mm a')

样本测试 运行 作为字符串:

const out = document.getElementById('output')
const testDate = "2022-02-11 17:41:39.330443"
const momentCode = moment(testDate).format('DD/MM/YY, h:mm a')
out.innerText = momentCode
<p id="output"></p>

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>