//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
}