ts,js收集常用日期,时间格式转换

6 篇文章 0 订阅

/*年-月-日- 时:分:秒*/
export function parseTime(time) {
  if (time) {
    let date = new Date(time)
    let year = date.getFullYear()
    /* 在日期格式中,月份是从0开始的,因此要加0
     * 使用三元表达式在小于10的前面加0,以达到格式统一  如 09:11:05
     * */
    let month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
    let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
    let hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
    let minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
    let seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
    // 拼接
    return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds
  } else {
    return ''
  }
}

// 月-日
export function parseMonthDay(time) {
  if (time) {
    let date = new Date(time)
    let month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
    let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
    // 拼接
    return month + '-' + day
  } else {
    return ''
  }
}
// 时-秒
export function parseTimeOper(time) {
  if (time) {
    let date = new Date(time)
    /* 在日期格式中,月份是从0开始的,因此要加0
     * 使用三元表达式在小于10的前面加0,以达到格式统一  如 09:11:05
     * */
    let hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
    let minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
    // 拼接
    return hours + ':' + minutes
  } else {
    return ''
  }
}

/*年-月-日- 时:分:秒*/
export function parseTime_CN(time) {
  if (time) {
    let week
    let date = new Date(time)
    let year = date.getFullYear()
    /* 在日期格式中,月份是从0开始的,因此要加0
     * 使用三元表达式在小于10的前面加0,以达到格式统一  如 09:11:05
     * */
    let month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
    let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
    let hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
    let minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
    let seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
    // 拼接
    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 year + '年' + month + '月' + day + '日 ' + hours + ':' + minutes + ':' + seconds + "\xa0\xa0" + week
  } else {
    return ''
  }
}

///*中文:年-月-日- 时:分:秒*/
export function parse_cn_time(time) {
  if (time) {
    let date = new Date(time)
    let year = date.getFullYear()
    /* *
     * 在日期格式中,月份是从0开始的,因此要加0
     * 使用三元表达式在小于10的前面加0,以达到格式统一  如 09:11:05
     * */
    let month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
    let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
    let hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
    let minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
    let seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
    return year + '年' + month + '月' + day + '日 ' + hours + ':' + minutes + ':' + seconds + "\xa0\xa0"
  } else {
    return ''
  }
}

///*中文:星期week*/
export function parse_cn_week(time) {
  if (time) {
    let week
    let date = new Date(time)
    // 拼接
    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
  } else {
    return ''
  }
}

/*年-月-日 时:分*/
export function parseTimeTwo(time) {
  if (time) {
    let date = new Date(time)
    let year = date.getFullYear()
    /* 在日期格式中,月份是从0开始的,因此要加0
     * 使用三元表达式在小于10的前面加0,以达到格式统一  如 09:11:05
     * */
    let month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
    let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
    let hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
    let minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
    // 拼接
    return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes
  } else {
    return ''
  }
}
/*格式: 日/月 例如24/10*/
export function parseDayMonth(time) {
  if (time) {
    let date = new Date(time)
    /* 在日期格式中,月份是从0开始的,因此要加0
     * 使用三元表达式在小于10的前面加0,以达到格式统一  如 09:11:05
     * */
    let month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
    let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
    // 拼接
    return day + '/' + month
  } else {
    return ''
  }
}
/*年-月-日*/
export function parseTimeThrid(time) {
  if (time) {
    let date = new Date(time)
    let year = date.getFullYear()
    /* 在日期格式中,月份是从0开始的,因此要加0
     * 使用三元表达式在小于10的前面加0,以达到格式统一  如 09:11:05
     * */
    let month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
    let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
    // 拼接
    return year + '-' + month + '-' + day
  } else {
    return ''
  }
}
/*年-月-日 时:分*/
export function parseTimeFour(time) {
  if (time) {
    let date = new Date(time)
    let year = date.getFullYear()
    /* 在日期格式中,月份是从0开始的,因此要加0
     * 使用三元表达式在小于10的前面加0,以达到格式统一  如 09:11:05
     * */
    let month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
    let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
    let hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
    // 拼接
    return year + '年' + month + '月' + day + '日' + hours + '时'
  } else {
    return ''
  }
}

/*年-月-日*/
export function parseTimeFive(time) {
  if (time) {
    let date = new Date(time)
    let year = date.getFullYear()
    /* 在日期格式中,月份是从0开始的,因此要加0
     * 使用三元表达式在小于10的前面加0,以达到格式统一  如 09:11:05
     * */
    let month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
    let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
    // 拼接
    return year + '年' + month + '月' + day+ '日'
  } else {
    return ''
  }
}
/*年/月/日 时:分*/
export function parseTimeSix(time) {
  if (time) {
    let date = new Date(time)
    let year = date.getFullYear()
    /* 在日期格式中,月份是从0开始的,因此要加0
     * 使用三元表达式在小于10的前面加0,以达到格式统一  如 09:11:05
     * */
    let month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
    let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
    let hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
    let minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
    // 拼接
    return year + '/' + month + '/' + day + ' ' + hours + ':'+ minutes
  } else {
    return ''
  }
}
/*当前日期增加n天*/
export function currentDateAddDate(num) {
  let d1 = new Date();
  let d2 = new Date(d1);
  d2.setDate(d1.getDate()+ num);
  return d2
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值