将秒数转换为 dd:hh:mm:ss 或天时分秒
converting seconds to dd:hh:mm:ss or Days Hours Mins Secs
我正在创建一个自定义管道,用于通过管道传递的布尔参数将秒数转换为 dd:hh:mm:ss 或天时分秒。这是我当前的代码(同样放置在自定义管道 class 的 transfrom 函数中):
let d=h=m=s=0 ;
let td, th,tm,ts ;
function transform(value, wordFormat = false){
h = Math.floor(value / 3600) ;
temp = value % 3600 ;
m = Math.floor( this.temp / 60) ;
s = this.temp % 60 ;
if (h > 24){
d = Math.floor( this.h / 24) ;
h = this.h % 24 ;
}
else{
d = 0 ;
}
if (wordFormat){
if (d){
td = `${d} days` ;
th = `${h} hours` ;
return td + ' ' + th ;
}
else{
if (h){
th = `${h} hours` ;
tm = `${m} mins` ;
return th + ' ' + tm ;
}
else{
if (m){
tm = `${m} mins` ;
return tm ;
}
else{
ts = `${s} secs` ;
return ts ;
}
}
}
}
else{
if (d){
td = `0${d}`.slice(-2) ;
th = `0${h}`.slice(-2) ;
tm = `0${m}`.slice(-2) ;
ts = `0${s}`.slice(-2) ;
return `${td}:${th}:${tm}:${ts}` ;
}
else{
if (h){
th = `0${h}`.slice(-2) ;
tm = `0${m}`.slice(-2) ;
ts = `0${s}`.slice(-2) ;
return `${th}:${tm}:${ts}` ;
}
else{
tm = `0${m}`.slice(-2) ;
ts = `0${s}`.slice(-2) ;
return `${tm}:${ts}` ;
}
}
}
}
document.write('word format: ' +transform(20000, true) + '<br>')
document.write('digit format: ' + transform(20000)+ '<br>')
document.write('Conversion is wrong(for decimal values): ' +transform(200.556456)+ '<br>')
我认为我对秒的基本转换只适用于整数形式,对于十进制值,它提供了错误的答案。如何修改我的函数以适用于输入的十进制和整数值。 (因为我的项目中提供的数据 may/may 不是整数);
试试这个。
transform(milliseconds: seconds, args?: any) {
var cd = 24 * 60 * 60 ,
ch = 60 * 60 ,
day = Math.floor(seconds / cd),
hr = Math.floor((seconds - day * cd) / ch),
min = Math.round((seconds - day * cd - hr * ch) / 60);
if (min === 60) {
hr++;
min = 0;
}
if (hr === 24) {
day++;
min = 0;
}
var valStr = (day != 0) ? day + " day(s) " : "";
valStr += (hr != 0) ? hr + " hr(s) " : "";
valStr += (min != 0) ? min + " min(s) " : "";
return valStr;
}
我不确定现实世界的用途或您希望在您的应用程序中使用的格式,但这可能有助于大大简化整个过程。
将其视为 Beta 测试方法和解决方案。
function transform( value, word ) {
with( new Date( value * 1000) )
var dd = getUTCDate() - 1,
hh = getUTCHours(),
mm = getUTCMinutes(),
ss = getUTCSeconds(),
ml = getUTCMilliseconds(),
dg = toUTCString().match(/\d{2}:\d{2}:\d{2}/);
return word ?
(dd ? dd + " days " : "") +
(hh ? hh + " hours ": "") +
(mm ? mm + " minutes ": "") +
(ss ? ss + " seconds ": "") +
(ml ? ml + " miliseconds " : "" )
:
(dd ? dd + ":" : "") + dg[0] + (ml ? "." + ml : "");
};
console.log("digital: " + transform( 85520.522, 0 ) );
console.log("words: " + transform( 85520.522, 1 ) );
console.log(transform( 567.12147, 1 ) );
console.log(transform( 12540, 1 ) );
console.log(transform( 12560.000, 1 ) );
console.log(transform( 23000.122, 1 ) );
console.log("digital: " + transform( (24*8640+27000.51), 0 ) );
console.log("words: " + transform( (24*8640+27000.51), 1 ) );
p.s.: 最大天数限制为 30 天,一旦超过,它将以月为单位转储。
以上所有答案都很好,尽管 Chris G 发现我只需要四舍五入秒值,这会弄乱整个时间字符串,所以 Math.round(value)
无需采用新方法即可解决此问题。
我正在创建一个自定义管道,用于通过管道传递的布尔参数将秒数转换为 dd:hh:mm:ss 或天时分秒。这是我当前的代码(同样放置在自定义管道 class 的 transfrom 函数中):
let d=h=m=s=0 ;
let td, th,tm,ts ;
function transform(value, wordFormat = false){
h = Math.floor(value / 3600) ;
temp = value % 3600 ;
m = Math.floor( this.temp / 60) ;
s = this.temp % 60 ;
if (h > 24){
d = Math.floor( this.h / 24) ;
h = this.h % 24 ;
}
else{
d = 0 ;
}
if (wordFormat){
if (d){
td = `${d} days` ;
th = `${h} hours` ;
return td + ' ' + th ;
}
else{
if (h){
th = `${h} hours` ;
tm = `${m} mins` ;
return th + ' ' + tm ;
}
else{
if (m){
tm = `${m} mins` ;
return tm ;
}
else{
ts = `${s} secs` ;
return ts ;
}
}
}
}
else{
if (d){
td = `0${d}`.slice(-2) ;
th = `0${h}`.slice(-2) ;
tm = `0${m}`.slice(-2) ;
ts = `0${s}`.slice(-2) ;
return `${td}:${th}:${tm}:${ts}` ;
}
else{
if (h){
th = `0${h}`.slice(-2) ;
tm = `0${m}`.slice(-2) ;
ts = `0${s}`.slice(-2) ;
return `${th}:${tm}:${ts}` ;
}
else{
tm = `0${m}`.slice(-2) ;
ts = `0${s}`.slice(-2) ;
return `${tm}:${ts}` ;
}
}
}
}
document.write('word format: ' +transform(20000, true) + '<br>')
document.write('digit format: ' + transform(20000)+ '<br>')
document.write('Conversion is wrong(for decimal values): ' +transform(200.556456)+ '<br>')
我认为我对秒的基本转换只适用于整数形式,对于十进制值,它提供了错误的答案。如何修改我的函数以适用于输入的十进制和整数值。 (因为我的项目中提供的数据 may/may 不是整数);
试试这个。
transform(milliseconds: seconds, args?: any) {
var cd = 24 * 60 * 60 ,
ch = 60 * 60 ,
day = Math.floor(seconds / cd),
hr = Math.floor((seconds - day * cd) / ch),
min = Math.round((seconds - day * cd - hr * ch) / 60);
if (min === 60) {
hr++;
min = 0;
}
if (hr === 24) {
day++;
min = 0;
}
var valStr = (day != 0) ? day + " day(s) " : "";
valStr += (hr != 0) ? hr + " hr(s) " : "";
valStr += (min != 0) ? min + " min(s) " : "";
return valStr;
}
我不确定现实世界的用途或您希望在您的应用程序中使用的格式,但这可能有助于大大简化整个过程。 将其视为 Beta 测试方法和解决方案。
function transform( value, word ) {
with( new Date( value * 1000) )
var dd = getUTCDate() - 1,
hh = getUTCHours(),
mm = getUTCMinutes(),
ss = getUTCSeconds(),
ml = getUTCMilliseconds(),
dg = toUTCString().match(/\d{2}:\d{2}:\d{2}/);
return word ?
(dd ? dd + " days " : "") +
(hh ? hh + " hours ": "") +
(mm ? mm + " minutes ": "") +
(ss ? ss + " seconds ": "") +
(ml ? ml + " miliseconds " : "" )
:
(dd ? dd + ":" : "") + dg[0] + (ml ? "." + ml : "");
};
console.log("digital: " + transform( 85520.522, 0 ) );
console.log("words: " + transform( 85520.522, 1 ) );
console.log(transform( 567.12147, 1 ) );
console.log(transform( 12540, 1 ) );
console.log(transform( 12560.000, 1 ) );
console.log(transform( 23000.122, 1 ) );
console.log("digital: " + transform( (24*8640+27000.51), 0 ) );
console.log("words: " + transform( (24*8640+27000.51), 1 ) );
p.s.: 最大天数限制为 30 天,一旦超过,它将以月为单位转储。
以上所有答案都很好,尽管 Chris G 发现我只需要四舍五入秒值,这会弄乱整个时间字符串,所以 Math.round(value)
无需采用新方法即可解决此问题。