123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- function formatActivity(seconds) {
- var h = parseInt(seconds / 3600) //小时
- var m = parseInt((seconds / 60) % 60) //分钟
- var s = parseInt(seconds % 60) //当前的秒
- h = h < 10 ? '0' + h : h
- m = m < 10 ? '0' + m : m
- s = s < 10 ? '0' + s : s
- return h + ':' + m + ':' + s
- }
- /**
- * 时间戳格式化函数
- * @param {string} format 格式 Y-m-d H:i:s
- * @param {int} timestamp 要格式化的时间 默认为当前时间
- * @return {string} 格式化的时间字符串
- */
- function timestampToDate(timestamp, format) {
- format = format || 'Y-m-d'
- let zero = function (value) {
- if (value < 10) {
- return '0' + value
- }
- return value
- }
- let myDate = timestamp ? new Date(timestamp * 1000) : new Date()
- let year = myDate.getFullYear()
- let month = zero(myDate.getMonth() + 1)
- let day = zero(myDate.getDate())
- let hour = zero(myDate.getHours())
- let minite = zero(myDate.getMinutes())
- let second = zero(myDate.getSeconds())
- return format.replace(/Y|m|d|H|i|s/gi, function (matches) {
- return {
- Y: year,
- m: month,
- d: day,
- H: hour,
- i: minite,
- s: second
- }[matches]
- })
- }
- module.exports = {
- formatActivity: formatActivity,
- timestampToDate: timestampToDate
- }
|