时间与时间戳的转换

时分转当天的时间戳

gettime(time){
    const start = time.split(':')
    const startime = new Date().setHours(start[0], start[1], 0, 0)
    return time
}

时间戳转时分 利用moment.js

function timestampTos (Timestamps) {
    return moment(Timestamps * 1000).format('HH:mm')
}
/**
 *
 *
 * 时间戳转时分秒
 * @param {number} Timestamps 时间戳
 * @returns {string} 23:59(分)
 */
export function timestampTos (Timestamps) {
    return moment(Timestamps * 1000).format('HH:mm')
}

/**
 * 时间戳转时分秒
 * @param {number} Timestamps 时间戳
 * @returns {string} 23:59:59(秒)
 */
export function timestampToms (Timestamps) {
    return moment(Timestamps * 1000).format('HH:mm:ss')
}

/**
 * 时间戳转日期
 * @param Timestamps 时间戳
 * @returns {string} 2018-08-28 (日)
 */
export function timestampToYMD (Timestamps) {
    if (Timestamps) {
        if (parseInt(Timestamps) === 1542011036 || parseInt(Timestamps) === 0) {
            return '--'
        } else {
            return moment(Timestamps * 1000).format('YYYY-MM-DD')
        }
    } else {
        return '--'
    }
}

/**
 * 时间戳转短时间
 * @param Timestamps 时间戳
 * @returns {string} 2018-08-28 23:59(分)
 */
export function timestampToYMDHm (Timestamps) {
    if (Timestamps && Timestamps !== '0') {
        return moment(Timestamps * 1000).format('YYYY-MM-DD HH:mm')
    } else {
        return '--'
    }
}

/**
 * 时间戳转长时间
 * @param Timestamps 时间戳
 * @returns {string} 2018-08-28 23:59:59(秒)
 */
export function timestampToYMDHms (Timestamps) {
    if (Timestamps) {
        return moment(Timestamps * 1000).format('YYYY-MM-DD HH:mm:ss')
    } else {
        return '--'
    }
}

***
 * 时间戳转为周几 10位时间戳
 */
export function timestampToWeek(time) {
  function timedat(res){   //res 为传入的时间戳   例:1509091800000

    var time = new Date(res);

    var y = time.getFullYear();

    var m = time.getMonth()+1;

    var d = time.getDate();

    return y+'-'+m+'-'+d;    //返回格式  "2017-10-27" 字符串
  };
  let date = new Date(timedat(time*1000))
  console.log(time,date)
  var week;
  if(date.getDay() == 0) week = "星期日"
  if(date.getDay() == 1) week = "星期一"
  if(date.getDay() == 2) week = "星期二"
  if(date.getDay() == 3) week = "星期三"
  if(date.getDay() == 4) week = "星期四"
  if(date.getDay() == 5) week = "星期五"
  if(date.getDay() == 6) week = "星期六"
  return week;
}
/**
 * 时间转时间戳
 * @param {number} time 时间
 * @returns {string}  1534567891(秒)
 */
export function timeToTimestamp (time) {
    return moment(time, 'YYYY-MM-DD HH:mm:ss').valueOf() / 1000
}

/**
 * 时间转时分
 * @param {number} time 时间
 * @returns {string}  23:59(分)
 */
export function timeToHm (time) {
    let Timestamps = moment(time, 'YYYY-MM-DD HH:mm').valueOf()
    return moment(Timestamps).format('HH:mm')
}

/**
 * 时间转时分秒
 * @param {number} time 时间
 * @returns {string}  23:59:59(秒)
 */
export function timeToHms (time) {
    let Timestamps = moment(time, 'YYYY-MM-DD HH:mm').valueOf()
    return moment(Timestamps).format('HH:mm:ss')
}

/**
 * 时间对象转时分秒
 * @param {number} time 时间
 * @return {string} 2018-08-08 23:59:59(秒)
 */
export function timeToYMDms (time) {
    let Timestamps = moment(time, 'YYYY-MM-DD HH:mm:ss').valueOf()
    return moment(Timestamps).format('YYYY-MM-DD HH:mm:ss')
}

/**
 * 时间对象转时分
 * @param {number} time 时间
 * @return {string} 2018-08-08 23:59(分)
 */
export function timeToYMDm (time) {
    let Timestamps = moment(time, 'YYYY-MM-DD HH:mm:ss').valueOf()
    return moment(Timestamps).format('YYYY-MM-DD HH:mm')
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值