时间格式化

//2021-06-22T06:08:45.000+0000 => 2021/06/22 06:08:45
function formatTime(dateString, split) {
	if (!dateString)
		return ''
	split = split ? split : '-'
	var replaceString = dateString.replace("T", " ")
	var reg = getRegExp("-", "g") 
	replaceString = replaceString.replace(reg, "/")
	var date = getDate(replaceString.substring(0, 19))
	date.setTime(date.setHours(date.getHours() + 8));

    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()
    var hour = date.getHours()
    var minute = date.getMinutes()
    var second = date.getSeconds()

    return [year, month, day].map(formatNumber).join(split) + ' ' + [hour, minute, second].map(formatNumber).join(':')
}


function getCurrentTime(split){
	split = split ? split : '/'
	var date = getDate()

	var year = date.getFullYear()
	var month = date.getMonth() + 1
	var day = date.getDate()
	var hour = date.getHours()
	var minute = date.getMinutes()
	var second = date.getSeconds()

	return [year, month, day].map(formatNumber).join(split) + ' ' + [hour, minute, second].map(formatNumber).join(':')
}


function formatTimeByMill(dateString, split) {
	if (!dateString)
		return ''
    split = split ? split : '/'
    var date = getDate(parseInt(dateString))
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()
    var hour = date.getHours()
    var minute = date.getMinutes()
    var second = date.getSeconds()

    return [year, month, day].map(formatNumber).join(split) + ' ' + [hour, minute, second].map(formatNumber).join(':')
}


//2021-06-22T06:08:45.000+0000 => 2021/06/22
function formatDate(dateString, split) {
	if (!dateString)
		return ''
	split = split ? split : '-'

	var replaceString = dateString.replace("T", " ")
	// console.log(replaceString)
	var reg = getRegExp("-", "g")
	replaceString = replaceString.replace(reg, "/")
	// console.log(replaceString)
	var date = getDate(replaceString.substring(0, 19))

	date.setTime(date.setHours(date.getHours() + 8));

    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()

    return [year, month, day].map(formatNumber).join(split)
}

function formatDateByMill(mill, split) {
	if (!mill)
		return ''
	split = split ? split : '/'
	var date = getDate(mill)
	// console.log(date)
	var year = date.getFullYear()
	var month = date.getMonth() + 1
	var day = date.getDate()

	return [year, month, day].map(formatNumber).join(split)
}

function getMillSecond(dateString) {
	if (!dateString)
		return ''
    var date = getDate(dateString)
    return date.getTime()
}

function formatNumber(n) {
    n = n.toString()
    return n[1] ? n : '0' + n
}

function formatTimeNew(dateString) {
	if (!dateString)
		return ''
    var date = getDate(dateString)
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()
    var hour = date.getHours()
    var minute = date.getMinutes()
    var second = date.getSeconds()
    return [hour, minute].map(formatNumber).join(':')
}
function formatDateMY(dateString, split){
    if (!dateString)
    return ''
split = split ? split : '/'
var replaceString = dateString ? dateString.replace(".000+0000", "Z") : ''
var date = getDate(replaceString)
var year = date.getFullYear()
var month = date.getMonth() + 1
var day = date.getDate()

return [month, day].map(formatNumber).join(split)
}
function selectWeekDay(day) {
	switch (day) {
		case '0':
			return '日'
		case '1':
			return '一'
		case '2':
			return '二'
		case '3':
			return '三'
		case '4':
			return '四'
		case '5':
			return '五'
		case '6':
			return '六'
		case '7':
			return '日'
	}
}

function formatTimeC(dateString){
	if(!dateString)
		return ''
		
		var pattern = getRegExp ('(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})');
		var Bdate = dateString.replace(pattern, '$1/$2/$3 $4:$5:$6');
		return Bdate
}



module.exports = {
    formatTime: formatTime,
    formatDate: formatDate,
    formatTimeNew: formatTimeNew,
    getMillSecond: getMillSecond,
    formatTimeByMill: formatTimeByMill,
  	getCurrentTime: getCurrentTime,
    formatDateByMill: formatDateByMill,
    formatDateMY:formatDateMY,
	selectWeekDay: selectWeekDay,
	formatTimeC:formatTimeC
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值