毫秒转换为天、小时、分、秒

/**
     *
     * @param 要转换的毫秒数
     * @return 该毫秒数转换为 * days(天) * hours(小时) * minutes(分) * seconds(秒) 后的格式
     * @author fy.zhang
     */
    public static String formatDuring(long mss) {
        long days = mss / (1000 * 60 * 60 * 24);
        long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
        long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
        long seconds = (mss % (1000 * 60)) / 1000;
        return days + " days " + hours + " hours " + minutes + " minutes "
                + seconds + " seconds ";
    }
    /**
     *
     * @param begin 时间段的开始
     * @param end   时间段的结束
     * @return  输入的两个Date类型数据之间的时间间格用* days * hours * minutes * seconds的格式展示
     * @author fy.zhang
     */
    public static String formatDuring(Date begin, Date end) {
        return formatDuring(end.getTime() - begin.getTime());
    }
--------------------- 
作者:毕设在线(QQ562138038) 
来源:CSDN 
原文:https://blog.csdn.net/mr_jianrong/article/details/72794194 
版权声明:本文为博主原创文章,转载请附上博文链接!

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaScript是一种基于Web的脚本语言,可以使用内置的函数和方法执行各种操作,包括将毫秒转换为年月日时毫秒是时间的一种度量单位,通常用来表示从1970年1月1日午夜(即Unix纪元)以来的毫秒数。要将毫秒转换为可读的日期和时间格式,可以使用JavaScript的Date对象和其内置的方法。 首先,需要将毫秒数传递给一个新的Date对象,并使用该对象的方法来提取日期和时间信息。例如,可以使用getDate()方法来提取日期、getFullYear()方法来提取年份、getHours()方法来提取小时等。然后,将这些值组合起来形成一个可读的日期和时间字符串。 以下是将毫秒转换为年月日时的JavaScript代码示例: ``` function formatMilliseconds(milliseconds) { // Create a new Date object with the milliseconds var date = new Date(milliseconds); // Extract the date and time information var year = date.getFullYear(); var month = date.getMonth() + 1; // Months start at 0 var day = date.getDate(); var hours = date.getHours(); var minutes = date.getMinutes(); var seconds = date.getSeconds(); // Format the date and time string var formattedDate = year + '-' + addZero(month) + '-' + addZero(day); var formattedTime = addZero(hours) + ':' + addZero(minutes) + ':' + addZero(seconds); var formattedString = formattedDate + ' ' + formattedTime; // Return the formatted string return formattedString; } function addZero(number) { // Adds a leading zero to single-digit numbers if (number < 10) { return '0' + number; } else { return number; } } // Example usage var milliseconds = 1546300800000; // January 1, 2019 var formattedString = formatMilliseconds(milliseconds); console.log(formattedString); // "2019-01-01 00:00:00" ``` 这个函数接受一个毫秒数作为参数,然后使用新的Date对象提取日期和时间信息。然后,函数格式化日期和时间字符串,确保月份、日、小时等始终用两位数表示。最后,函数返回格式化后的字符串。在这个例子中,我们将一个代表2019年1月1日午夜的毫秒数传递给函数,并获得一个形如“2019-01-01 00:00:00”的字符串作为输出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值