js 毫秒 微秒 转为 时分秒

搜了那么多,全是用parseInt 取整,是不对的
1/9999999 结果为 1.00000010000001e-7
parseInt(1/9999999) 结果为 1, 所以取Math.floor 就可以了

export function setDateTimePrefix (dateTime: number): string {
    return dateTime < 10 ? `0${dateTime}` : `${dateTime}`;
}
 //  传进来是 微秒
export function setBackLogTime (us: AnyType) {
	
    if (!us) return '00:00:00.0';
    // 微秒 转为  毫秒
    const mss = Math.floor(us / 1000);
    if (!mss) return '00:00:00.0';
    const days = Math.floor(mss / (1000 * 60 * 60 * 24));
    const hours = Math.floor((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    const minutes = Math.floor((mss % (1000 * 60 * 60)) / (1000 * 60));
    const seconds = Math.floor((mss % (1000 * 60)) / 1000);
    const ms = (mss % (1000 * 60)) % 1000;
    const res = setDateTimePrefix(hours) + ':' + setDateTimePrefix(minutes) + ':' + setDateTimePrefix(seconds) + '.' + ms;
    return days ? days + 'day ' + res : res;
}
### Java 中去除 `Date` 对象微秒部分的方法 在 Java 中处理日期和时间时,有时需要移除 `Date` 对象中的微秒部分。一种常见的方式是通过格式化字符串来达到这一目的。 对于 `java.util.Date` 类型的对象,可以通过使用 `SimpleDateFormat` 来控制输出格式并间接清除微秒部分: ```java import java.text.SimpleDateFormat; import java.util.Date; public class Main { public static void main(String[] args) { // 创建日期对象 Date date = new Date(); // 定义不带毫秒的日期格式 SimpleDateFormat sdfWithoutMillis = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 将日期转换成指定格式的字符串再解析回日期对象 String formattedDateStr = sdfWithoutMillis.format(date); try { Date dateWithoutMillis = sdfWithoutMillis.parse(formattedDateStr); System.out.println("去除了微秒后的日期:" + dateWithoutMillis); } catch (Exception e) { e.printStackTrace(); } } } ``` 上述代码展示了如何利用 `SimpleDateFormat` 的格式化功能来构建一个新的不含微秒信息的 `Date` 实例[^3]。 另一种方式是在创建新的 `Timestamp` 或者 `Date` 对象之前先调整其对应的毫秒数值,使其代表整秒时刻,这样也可以有效地消除微秒的影响[^1]。 需要注意的是,在某些情况下直接操作底层的时间戳可能不是最佳实践;如果应用程序逻辑允许的话,推荐采用更高级别的 API 如 `LocalDateTime`, `ZonedDateTime` 等来自 `java.time` 包下的类来进行此类操作,因为这些新引入的数据类型提供了更好的可读性和功能性支持[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值