You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用split()从AWSDateTime中提取时分(不含秒)

如何用.split()从AWSDateTime中提取HH:MM格式时间?

你需要从AWSDateTime格式的字符串中提取仅包含小时和分钟的部分,适配HTML的time类型。

示例数据

数据库中的AWSDateTime示例:

startDateTime = '2022-11-11T11:11:11.482Z'

原有代码及问题

你原本的代码:

this.startTime = this.edit.onHold[0].startDateTime.split('T')[1];

当前返回结果:

11:11:11.482Z 

但你需要的结果是:

11:11

解决方案

可以通过多次split()组合实现需求,也可以搭配字符串截取简化操作:

方案1:多次.split()拼接

先通过'T'分割拿到时间部分,再用':'分割出小时、分钟项,最后拼接成目标格式:

// 分步写法
const timePart = this.edit.onHold[0].startDateTime.split('T')[1];
this.startTime = timePart.split(':').slice(0, 2).join(':');

// 链式调用写法
this.startTime = this.edit.onHold[0].startDateTime.split('T')[1].split(':').slice(0, 2).join(':');

方案2:先去毫秒再截取

先通过'.'分割去掉毫秒及之后的内容,再截取前5位字符(HH:MM固定长度为5):

this.startTime = this.edit.onHold[0].startDateTime.split('T')[1].split('.')[0].substring(0, 5);

方案3:直接截取固定位置

拿到时间部分后,直接截取前5位字符,最简洁:

this.startTime = this.edit.onHold[0].startDateTime.split('T')[1].substring(0, 5);

内容的提问来源于stack exchange,提问作者DZF

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 17:15:13