方法一:
個人做法是保存時間戳,然后在前端用jq插件做轉換,比如 smart-time-ago
方法二:
(通過freemarker模板)如果用freemarker模板可以這樣寫,別的模板類推
根據自己的意愿修改條件和輸出,把你的datetime傳進去即可
<#macro timeline_dt datetime=.now> <#assign ct = (.now?long-datetime?long)/1000> <#if ct gte 31104000><#--n年前-->${(ct/31104000)?int}年前 <#t><#elseif ct gte 2592000><#--n月前-->${(ct/2592000)?int}個月前 <#t><#elseif ct gte 86400*2><#--n天前-->${(ct/86400)?int}天前 <#t><#elseif ct gte 86400><#--1天前-->昨天 <#t><#elseif ct gte 3600><#--n小時前-->${(ct/3600)?int}小時前 <#t><#elseif ct gte 60><#--n分鐘前-->${(ct/60)?int}分鐘前 <#t><#elseif ct gt 0><#--n秒前-->${ct?int}秒前 <#t><#else>剛剛 #if> #macro>
方法三:
找到一個專門的插件PrettyTime
方法四:
自定義Java方法:
方法五:
使用js插件:(原版的timeago.js)
使用js插件:(改裝版(簡喲版)timeago.js)中文的
希望本文所述對大家的javascript程序設計有所幫助。
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com