mirror of
https://github.com/jerryc127/hexo-theme-butterfly.git
synced 2026-04-10 21:17:07 +08:00
fix: 修复 说说时间 timezone bug
This commit is contained in:
@@ -21,13 +21,35 @@
|
|||||||
let start = 0
|
let start = 0
|
||||||
const container = document.getElementById('article-container')
|
const container = document.getElementById('article-container')
|
||||||
|
|
||||||
const showDateAndTine = date => new Date(date).toLocaleString()
|
const getTimeZoneDate = date => {
|
||||||
|
if (date.length === 10) {
|
||||||
|
date = date + ' 00:00:00'
|
||||||
|
}
|
||||||
|
|
||||||
|
const visitorTimeZone = '#{config.timezone}' || Intl.DateTimeFormat().resolvedOptions().timeZone
|
||||||
|
const options = {
|
||||||
|
timeZone: visitorTimeZone,
|
||||||
|
year: 'numeric',
|
||||||
|
month: '2-digit',
|
||||||
|
day: '2-digit',
|
||||||
|
hour: '2-digit',
|
||||||
|
minute: '2-digit',
|
||||||
|
second: '2-digit',
|
||||||
|
hour12: false
|
||||||
|
}
|
||||||
|
|
||||||
|
const formattedDate = new Intl.DateTimeFormat('en-GB', options).format(new Date(date))
|
||||||
|
|
||||||
|
const [day, month, year, hour, minute, second] = formattedDate.match(/\d+/g)
|
||||||
|
return `${year}-${month}-${day} ${hour}:${minute}:${second}`
|
||||||
|
}
|
||||||
|
|
||||||
const addData = data => {
|
const addData = data => {
|
||||||
const cLength = data.length
|
const cLength = data.length
|
||||||
const end = start + 10 > cLength ? cLength : start + 10
|
const end = start + 10 > cLength ? cLength : start + 10
|
||||||
let result = ''
|
let result = ''
|
||||||
data.slice(start, end).forEach((item) => {
|
data.slice(start, end).forEach((item) => {
|
||||||
|
const getDate = getTimeZoneDate(item.date)
|
||||||
result += `
|
result += `
|
||||||
<div class="shuoshuo-item">
|
<div class="shuoshuo-item">
|
||||||
<div class="shuoshuo-item-header">
|
<div class="shuoshuo-item-header">
|
||||||
@@ -36,7 +58,7 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="shuoshuo-info">
|
<div class="shuoshuo-info">
|
||||||
<div class="shuoshuo-author">${item.author || '!{config.author}'}</div>
|
<div class="shuoshuo-author">${item.author || '!{config.author}'}</div>
|
||||||
<time class="shuoshuo-date" title="${showDateAndTine(item.date)}">${btf.diffDate(item.date, true)}</time>
|
<time class="shuoshuo-date" title="${getDate}">${btf.diffDate(getDate, true)}</time>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="shuoshuo-content">
|
<div class="shuoshuo-content">
|
||||||
@@ -99,7 +121,8 @@
|
|||||||
img.no-lightbox(src=i.avatar || url_for(theme.avatar.img))
|
img.no-lightbox(src=i.avatar || url_for(theme.avatar.img))
|
||||||
.shuoshuo-info
|
.shuoshuo-info
|
||||||
.shuoshuo-author=i.author || config.author
|
.shuoshuo-author=i.author || config.author
|
||||||
time.shuoshuo-date(title=full_date(i.date))=date(i.date)
|
- const getDate = getTimeZoneDate(i.date)
|
||||||
|
time.shuoshuo-date(title=getDate)=date(getDate)
|
||||||
.shuoshuo-content
|
.shuoshuo-content
|
||||||
!=markdown(i.content)
|
!=markdown(i.content)
|
||||||
.shuoshuo-footer
|
.shuoshuo-footer
|
||||||
|
|||||||
@@ -3,6 +3,14 @@
|
|||||||
const { truncateContent, postDesc } = require('../common/postDesc')
|
const { truncateContent, postDesc } = require('../common/postDesc')
|
||||||
const { prettyUrls } = require('hexo-util')
|
const { prettyUrls } = require('hexo-util')
|
||||||
const crypto = require('crypto')
|
const crypto = require('crypto')
|
||||||
|
const moment = require('moment-timezone')
|
||||||
|
|
||||||
|
hexo.extend.helper.register('getTimeZoneDate', date => {
|
||||||
|
// This is a hack method, because hexo treats time as UTC time
|
||||||
|
// so you need to manually convert the time zone
|
||||||
|
const utcDate = moment.utc(date).format('YYYY-MM-DD HH:mm:ss')
|
||||||
|
return moment.tz(utcDate, hexo.config.timezone).format('YYYY-MM-DD HH:mm:ss')
|
||||||
|
})
|
||||||
|
|
||||||
hexo.extend.helper.register('truncate', truncateContent)
|
hexo.extend.helper.register('truncate', truncateContent)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user