// 中国 东8区 韩国 东9区 印度 东5.5区

export const getNewDate = function({ date, zone }) { // date 日期格式 zone 时区

var timezone = zone || 8; //目标时区时间

var dates = date || new Date()

var offset_GMT = dates.getTimezoneOffset(); // Date中时间和格林威治的时间差,单位为分钟

var nowDate = dates.getTime(); // Date中时间距 1970 年 1 月 1 日午夜(GMT 时间)之间的毫秒数

var targetDate = new Date(nowDate + offset_GMT * 60 * 1000 + timezone * 60 * 60 * 1000);

// console.log(`东${timezone}区现在是:` + targetDate)

return targetDate

}

export const toTimestamp = function(time, zone) { // time 可以为时间戳,可为日期

if (!time) return null

if (arguments.length === 0) {

return null

}

let date

if (typeof time === 'object') {

date = time

} else {

if ((typeof time === 'string')) {

if ((/^[0-9]+$/.test(time))) {

// support "1548221490638"

time = parseInt(time)

} else {

// support safari

// https://stackoverflow.com/questions/4310953/invalid-date-in-safari

time = time.replace(new RegExp(/-/gm), '/')

}

}

if ((typeof time === 'number') && (time.toString().length === 10)) {

time = time * 1000

}

date = new Date(time)

}

date = getNewDate({ date, zone })

return date.getTime()

}

export function parseTime(time, cFormat, zone) { // time 可以为时间戳,可为日期

if (!time) return null

if (arguments.length === 0) {

return null

}

const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'

let date

if (typeof time === 'object') {

date = time

} else {

if ((typeof time === 'string')) {

if ((/^[0-9]+$/.test(time))) {

// support "1548221490638"

time = parseInt(time)

} else {

// support safari

// https://stackoverflow.com/questions/4310953/invalid-date-in-safari

time = time.replace(new RegExp(/-/gm), '/')

}

}

if ((typeof time === 'number') && (time.toString().length === 10)) {

time = time * 1000

}

date = new Date(time)

}

date = getNewDate({ date, zone })

const formatObj = {

y: date.getFullYear(),

m: date.getMonth() + 1,

d: date.getDate(),

h: date.getHours(),

i: date.getMinutes(),

s: date.getSeconds(),

a: date.getDay()

}

const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {

const value = formatObj[key]

// Note: getDay() returns 0 on Sunday

if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value ] }

return value.toString().padStart(2, '0')

})

return time_str

}

/**

* @param {number} time

* @param {string} option

* @returns {string}

*/

export function formatTime(time, option) { // time 时间戳 option

if (('' + time).length === 10) {

time = parseInt(time) * 1000

} else {

time = +time

}

const d = new Date(time)

const now = Date.now()

const diff = (now - d) / 1000

if (diff < 30) {

return '刚刚'

} else if (diff < 3600) {

// less 1 hour

return Math.ceil(diff / 60) + '分钟前'

} else if (diff < 3600 * 24) {

return Math.ceil(diff / 3600) + '小时前'

} else if (diff < 3600 * 24 * 2) {

return '1天前'

}

if (option) {

return parseTime(time, option)

} else {

return (

d.getMonth() +

1 +

'月' +

d.getDate() +

'日' +

d.getHours() +

'时' +

d.getMinutes() +

'分'

)

}

}

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐