900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > js中国标准时间转年月日时分秒-时间戳转年月日

js中国标准时间转年月日时分秒-时间戳转年月日

时间:2019-06-22 23:24:45

相关推荐

js中国标准时间转年月日时分秒-时间戳转年月日

export function dateFormat(fmt, date) {let retconst opt = {'Y+': date.getFullYear().toString(), // 年'm+': (date.getMonth() + 1).toString(), // 月'd+': date.getDate().toString(), // 日'H+': date.getHours().toString(), // 时'M+': date.getMinutes().toString(), // 分'S+': date.getSeconds().toString(), // 秒// 有其他格式化字符需求可以继续添加,必须转化成字符串}for (let k in opt) {ret = new RegExp('(' + k + ')').exec(fmt)if (ret) {fmt = fmt.replace(ret[1], ret[1].length == 1 ? opt[k] : opt[k].padStart(ret[1].length, '0'))}}return fmt}// 用法// let date = new Date()// dateFormat("YYYY-mm-dd HH:MM", date)// >>> -06-06 19:45`// 1626772706000 ---> Tue Jul 20 17:18:26 GMT+0800 (中国标准时间)function timestampToDate(time) {var d = new Date()d.setTime(time)return d}// 1626772706000 ---> Tue Jul 20 17:18:26 GMT+0800 (中国标准时间)--->"-07-20 17:18"export function timestampFormatDate(timestamp, fmt) {const date = timestampToDate(timestamp)const formatData = dateFormat(fmt, date)return formatData}

js根据当前时区获取当前北京时间

思路:获取当前UTC时间,然后根据UCT时间计算北京时间

new Date(new Date().getTime()+(parseInt(new Date().getTimezoneOffset()/60) + 8)*3600*1000);}//Fri May 20 10:24:59 GMT+0800 (中国标准时间)//获取时间戳 1653013480061new Date(new Date().getTime()+(parseInt(new Date().getTimezoneOffset()/60) + 8)*3600*1000).getTime()

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。