如何将数字转换为时间?

我正在尝试使用 date-fns 1.30.1 版或纯 JavaScript 创建一个接受数字并返回时间戳 (HH:mm) 的函数。


我想要实现的是在输入时间时帮助用户。当用户离开输入字段时,我使用 Vue.js 来更新该字段。因此,如果用户键入 21 然后离开,则该字段最好更新为 21:00。


一些例子是:


21 = 21:00  

1 = 01:00  

24 = 00:00  

2115 = 21:15  

像 815 这样的数字不必返回 08:15。像 7889 这样的数字应该返回一个错误。


我试过使用正则表达式:


time = time

    .replace(/^([1-9])$/, '0$1')

    .replace(/^([0-9]{2})([0-9]+)$/, '$1:$2')

    .replace(/^24/, '00:00')

我也尝试过在 date-fns 中使用 parse 方法,但似乎无法解决如何解决这个问题。


哈士奇WWW
浏览 906回答 2
2回答

胡说叔叔

转换基于<100(仅小时)和>=100(100*小时+分钟),加上一些与24单位数(小时和分钟)的斗争:function num2time(num){&nbsp; var h,m="00";&nbsp; if(num<100)&nbsp; &nbsp; h=num;&nbsp; else {&nbsp; &nbsp; h=Math.floor(num/100);&nbsp; &nbsp; m=("0"+(num%100)).slice(-2);&nbsp; }&nbsp; h=h%24;&nbsp; return ("0"+h).slice(-2)+":"+m;}console.log(num2time(8));console.log(num2time(801));console.log(num2time(24));console.log(num2time(2401));console.log(num2time(2115));console.log(num2time("8"));console.log(num2time("2115"));原始答案,仅用于评论,但无法24正确处理或单位数分钟:例如,您可以进行非常机械的转换function num2time(num){&nbsp; if(num<10)&nbsp; &nbsp; t="0"+num+":00";&nbsp; else if(num<100)&nbsp; &nbsp; t=num+":00";&nbsp; else {&nbsp; &nbsp; if(num<1000)&nbsp; &nbsp; &nbsp; t="0"+Math.floor(num/100);&nbsp; &nbsp; else if(num<2400)&nbsp; &nbsp; &nbsp; t=Math.floor(num/100)&nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; t="00";&nbsp; &nbsp; t+=":"+(num%100);&nbsp; }&nbsp; return t;}console.log(num2time(8));console.log(num2time(2115));console.log(num2time("8"));console.log(num2time("2115"));示例验证:function num2time(num){&nbsp; var h,m="00";&nbsp; if(num<100)&nbsp; &nbsp; h=num;&nbsp; else {&nbsp; &nbsp; h=Math.floor(num/100);&nbsp; &nbsp; m=("0"+(num%100)).slice(-2);&nbsp; }&nbsp; if(h<0 || h>24) throw "Hour must be between 0 and 24"&nbsp; if(m<0 || m>59) throw "Minute must be between 0 and 59"&nbsp; h=h%24;&nbsp; return ("0"+h).slice(-2)+":"+m;}var numstr=prompt("Enter time code");while(true) {&nbsp; try {&nbsp; &nbsp; console.log(num2time(numstr));&nbsp; &nbsp; break;&nbsp; } catch(ex) {&nbsp; &nbsp; numstr=prompt("Enter time code, "+numstr+" is not valid\n"+ex);&nbsp; }}

慕工程0101907

DateFns 实现恕我直言,致力于添加和删除分钟和小时是管理此转换的更简洁的方法:function formattedTime(val) {&nbsp; let helperDate;&nbsp;&nbsp; if(val.length <= 2) {&nbsp; &nbsp;if(val > 24)return 'error';&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;helperDate = dateFns.addHours(new Date(0), val-1);&nbsp; &nbsp;&nbsp; &nbsp;return dateFns.format(helperDate, 'HH:mm');&nbsp; }&nbsp; if(val.length > 2) {&nbsp; &nbsp;let hhmm = val.match(/.{1,2}/g);&nbsp; &nbsp;if(hhmm[0] > 24 || hhmm[1] > 60) return 'error';&nbsp; &nbsp;helperDate = dateFns.addHours(new Date(0), hhmm[0]-1);&nbsp; &nbsp;helperDate = dateFns.addMinutes(helperDate, hhmm[1]);&nbsp; &nbsp;return dateFns.format(helperDate, 'HH:mm');&nbsp; }}const myArr = [21, 1, 24, 2115, 815];myArr.forEach(&nbsp; val => console.log(formattedTime(val.toString())))<script src="https://cdnjs.cloudflare.com/ajax/libs/date-fns/1.30.1/date_fns.min.js"></script>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript